티스토리 툴바


posted by ppiazi 2012/01/12 13:14
Creative Commons License
Creative Commons License
http://cstrings.blogspot.com/

윈도우쪽 프로그래밍을 전혀 손놨다가 근래 엄청 디었다..
저작자 표시 비영리 동일 조건 변경 허락
TAG
posted by ppiazi 2011/03/16 15:03
Creative Commons License
Creative Commons License
http://andromedarabbit.net/wp/timimg/


저작자 표시 비영리 동일 조건 변경 허락
posted by ppiazi 2010/12/14 23:44
Creative Commons License
Creative Commons License
다음에 참고하기 위해 쓴다.

#define    DebugRtiData(name)    do { \
    for (int i = 0; i < data_seq.length(); ++i) { \
        if (info_seq[i].valid_data) { \
            name##TypeSupport::print_data(&data_seq[i]); \
        } \
    }    \
    } while ( 0 )

사용예

DebugRtiData(FCE_AIDLC_CTRL);
저작자 표시 비영리 동일 조건 변경 허락
posted by ppiazi 2010/09/19 15:02
Creative Commons License
Creative Commons License
다음과 같은 코드가 있다고 하자.
루프를 돌면서 값이 value와 같다면 해당 원소를 map에서 지우는 것을 시도하는 코드이다.

typedef std::map<std::string, float> StringFloatMap;

StringFloatMap coll;
StringFloatMap::iterator pos;

for ( pos = coll.begin(); pos != coll.end(); ++pos)
{
     if ( pos->second == value )
     {
            coll.erase(pos);  // 런타임 에러!!
     }
}

coll.erase(pos)이후의 coll의 iterator인 pos가 무효화되므로, 런타임 에러가 발생하게 된다.

해결책은 아래와 같다.
typedef std::map<std::string, float> StringFloatMap;

StringFloatMap coll;
StringFloatMap::iterator pos;

for ( pos = coll.begin(); pos != coll.end(); ++pos)
{
     if ( pos->second == value )
     {
          coll.erase(pos++);
    }
     else
    {
          ++pos;
    }
}


STL과 관련된 참고자료는 아래의 링크를 참고한다.


저작자 표시 비영리 동일 조건 변경 허락
TAG
posted by ppiazi 2009/10/09 09:20
Creative Commons License
Creative Commons License

 

CppUnit #

  • CppUnit는 C++용 테스팅 프레임워크이다. Michael Feathers씨가 Java의 JUnit을 C++로 구현한 것이다.
  • 다음과 같은 특징을 갖는다.

    • XML output with hooks for additional data (XSL format avaliable in release 1.10.2 needs some FiXing)

    • Compiler-like text output to integrate with an IDE

    • Helper macros for easier test suite declaration

    • Hierarchical test fixture support

    • Test registry to reduce recompilation need

    • Test plug-in for faster compile/test cycle (self testable dynamic library)

    • Protector to encapsulate test execution (allow capture of exception not derived from std::exception)

    • MfcTestRunner

    • QtTestRunner, a Qt based graphic test runner

    • QxTestRunner, a Qt4 based graphic test runner

    • CursesTestRunner

    • WxWidgetsTestRunner (formerly: WxWindowsTestRunner)

CppUnit 다운로드#

  • http://sourceforge.net/projects/cppunit 에서 최근 파일을 다운로드한다.
  • 적당한 곳에 압축을 푼다. 압축을 푼곳의 디렉토리를 편의상 $CPPUNIT 이라고 칭한다.

VC++ 설정 하기#

CppUnit 라이브러리 빌드하기 #

  • $CPPUNIT/src/CppUnitLibraries.dsw를 VC++을 사용하여 연다.
  • Build메뉴에서 "Batch Build..."를 선택한다.
  • 라이브러리 빌드가 끝나면 $CPPUNIT/lib 디렉토리에 저장될 것이다.

VC++ 디렉토리 설정하기#

  • Tools/Options 메뉴를 설정한다.
  • Directories 탭을 선택하여 다음과 같이 설정한다.

    • Library Files 에 $CPPUNIT/lib 를 추가한다.
    • Header Files 에 $CPPUNIT/include 를 추가한다.

간단 사용기#

시작하기#

  • 새로운 프로젝트를 만든다.(콘솔 어플리케이션으로 한다.)
  • Project setting에서 다음을 수정한다.

    • C/C++ 탭에서 Code Generation을 선택하여 Release은 Multithreaded DLL을 Debug는 Debug Multithreaded DLL을 선택한다.
    • C/C++ 탭에서 C++ Language을 선택하여 Enable Run-Time Type Information(RTTI)을 체크한다.
    • Link 탭에서 Object/Library 모듈 필드에 Release는 cppunit.lib를, Debug는 cppunitd.lib를 추가해 준다.

테스트 수행 코드 만들기#

  • 이곳에서는 post-build testing을 위해 CompilerOutputter를 사용하는TextTestRunner를 선택할 것이다.
#include "stdafx.h"
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main(int argc, char* argv[])
{
  // Get the top level suite from the registry
  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

  // Adds the test to the list of test to run
  CppUnit::TextUi::TestRunner runner;
  runner.addTest( suite );

  // Change the default outputter to a compiler error format outputter
  runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
                                                       std::cerr ) );
  // Run the tests.
  bool wasSucessful = runner.run();

  // Return error code 1 if the one of test failed.
  return wasSucessful ? 0 : 1;
}

 

테스트 코드 추가하기#

  • 다음의 헤더파일과 소스파일을 추가한다.

MoneyTest.h

#if !defined(AFX_MONEYTEST_H__833FAB42_8C68_44A7_8B27_A70007883742__INCLUDED_)
#define AFX_MONEYTEST_H__833FAB42_8C68_44A7_8B27_A70007883742__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <cppunit/extensions/HelperMacros.h>

class MoneyTest : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(MoneyTest);    ///< declares that our Fiture's test suite
    CPPUNIT_TEST(testConstructor);    ///< adds a test to our test suite. The test is implemented by a method named testConstructor()
    CPPUNIT_TEST_SUITE_END();
public:
    MoneyTest();
    virtual ~MoneyTest();

    void setUp();        ///< setUp some fixtures
    void tearDown();    ///< tearDown some fixtures

    void testConstructor();

};

#endif // !defined(AFX_MONEYTEST_H__833FAB42_8C68_44A7_8B27_A70007883742__INCLUDED_)

 

MoneyTest.cpp

#include "stdafx.h"
#include "MoneyTest.h"

/// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(MoneyTest);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MoneyTest::MoneyTest()
{ }

MoneyTest::~MoneyTest()
{ }

void MoneyTest::setUp()
{ }

void MoneyTest::tearDown()
{ }

void MoneyTest::testConstructor()
{
    CPPUNIT_FAIL("not implemented");
}

 

  • 출력결과는 다음과 같다.

result1.JPG

 

레퍼런스

 

이 글은 스프링노트에서 작성되었습니다.

TAG
posted by ppiazi 2009/09/22 19:57
Creative Commons License
Creative Commons License
cloc --by-file --csv --out=result.csv "소스위치"




posted by ppiazi 2009/09/13 19:01
Creative Commons License
Creative Commons License
MingW를 설치하면, pthread library가 설치되지 않으므로 pthread 기능을 사용할 수 없다.
본 글은 eclipse(CDT) + MingW에서 pthread를 사용하기 위한 설정법을 기술한다.
eclipse(CDT)는 이미 설치되어 있다고 가정하고 있다.

1. 최신 pthread-win32를 받는다.

2. 기 빌드된 파일들을 이동한다.
   압축을 해제하면, 다음의 폴더구조를 확인할 수 있다.
   [압축해제폴더]\Pre-built.2          <-- 미리 빌드된 헤더와 라이브러리를 포함한 폴더
   [압축해제폴더]\pthreads.2          <-- 소스를 포함한 폴더
   [압축해제폴더]\QueueUserAPCEx
  • 헤더 파일을 이동한다.
    • [압축해제폴더]\Pre-built.2\include 에 있는 헤더파일을 [MingW Root]\include 로 복사한다.
  • 라이브러리 파일을 이동한다.
    • [압축해제폴더]\Pre-built.2\lib의 libpthreadGC2.a와 libpthreadGCE2.a 파일을 [MingW Root]\lib 로 복사한다.
    • [압축해제폴더]\Pre-built.2\lib의 libpthreadGC2.dll와 libpthreadGCE2.dll 을 dll 패스가 잡혀있는 곳으로 복사한다. ex) C:\WINDOWS\system32

3. eclipse 프로젝트 세팅을 한다.
   eclipse를 사용하여 예저 C Project를 생성한다. 프로젝트가 완성되면, 다음의 main.c 를 추가한다. 예제 소스는 Beginning Linux Program Chapter 12의 thread1.c 를 약간 수정하였다.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <windows.h>    // Sleep를 위해서 포함함
#define sleep(seconds)    Sleep((seconds)*1000)

void *thread_function(void *arg);

char message[] = "Hello World";

int main()
{
    int res;
    pthread_t a_thread;
    void *thread_result;

    printf("Try to create a thread\n");
    res = pthread_create(&a_thread, NULL, thread_function, (void *)message);

    if ( res != 0 )
    {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }

    printf("Wating for thread to finish...\n");
    res = pthread_join(a_thread, &thread_result);
    if ( res != 0 )
    {
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }

    printf("Thread joined, it returned %s\n", (char *)thread_result);
    printf("Message is now %s\n", message);
    exit(EXIT_SUCCESS);
}

void* thread_function(void *arg)
{
    printf("Thread function is running. Arg was %s\n", (char *)arg);
    sleep(3);
    strcpy(message, "Bye!");
    pthread_exit("Thank you for the CPU time");
}


   빌드를 하기전, 컴파일러/링커 세팅을 한다.
3.1 컴파일러 세팅
사용자 삽입 이미지

3.2 링커 세팅
사용자 삽입 이미지


4. 예제 프로그램을 수행하여 동작여부를 확인한다.
사용자 삽입 이미지

posted by ppiazi 2009/07/20 00:45
Creative Commons License
Creative Commons License
makefile과 관련된 링크를 모아놓았다.


저작자 표시 비영리 동일 조건 변경 허락
posted by ppiazi 2009/07/14 10:12
Creative Commons License
Creative Commons License

#include <stdio.h>

int main()
{
    int i = 0;

    for (i = 0; i < 5; i ++ )
    {
        printf("Hello World\n");
    }

    return 0;
}

위와 같은 소스 코드를 컴파일하여 어셈블리 코드와 소스 코드를 보는 방법은 아래와 같다.

gcc -c -g hello.cpp
와 같이 컴파일하여 오브젝트 코드를 얻는다.

objdump -dS hello.o > hello.txt
와 하면 아래와 같이 어셈블리 코드와 소스 코드를 한번에 확인할 수 있다.

test.o:     file format elf32-i386

Disassembly of section .init:

08048348 <_init>:
 8048348:    55                       push   %ebp
 8048349:    89 e5                    mov    %esp,%ebp
 804834b:    53                       push   %ebx
 804834c:    83 ec 04                 sub    $0x4,%esp
 804834f:    e8 00 00 00 00           call   8048354 <_init+0xc>
 8048354:    5b                       pop    %ebx
 8048355:    81 c3 88 13 00 00        add    $0x1388,%ebx
 804835b:    8b 93 fc ff ff ff        mov    -0x4(%ebx),%edx
 8048361:    85 d2                    test   %edx,%edx
 8048363:    74 05                    je     804836a <_init+0x22>
 8048365:    e8 1e 00 00 00           call   8048388 <__gmon_start__@plt>
 804836a:    e8 c1 00 00 00           call   8048430 <frame_dummy>
 804836f:    e8 9c 01 00 00           call   8048510 <__do_global_ctors_aux>
 8048374:    58                       pop    %eax
 8048375:    5b                       pop    %ebx
 8048376:    c9                       leave 
 8048377:    c3                       ret   
Disassembly of section .plt:

08048378 <__gmon_start__@plt-0x10>:
 8048378:    ff 35 e0 96 04 08        pushl  0x80496e0
 804837e:    ff 25 e4 96 04 08        jmp    *0x80496e4
 8048384:    00 00                    add    %al,(%eax)
    ...

08048388 <__gmon_start__@plt>:
 8048388:    ff 25 e8 96 04 08        jmp    *0x80496e8
 804838e:    68 00 00 00 00           push   $0x0
 8048393:    e9 e0 ff ff ff           jmp    8048378 <_init+0x30>

08048398 <__libc_start_main@plt>:
 8048398:    ff 25 ec 96 04 08        jmp    *0x80496ec
 804839e:    68 08 00 00 00           push   $0x8
 80483a3:    e9 d0 ff ff ff           jmp    8048378 <_init+0x30>

080483a8 <puts@plt>:
 80483a8:    ff 25 f0 96 04 08        jmp    *0x80496f0
 80483ae:    68 10 00 00 00           push   $0x10
 80483b3:    e9 c0 ff ff ff           jmp    8048378 <_init+0x30>

080483b8 <__gxx_personality_v0@plt>:
 80483b8:    ff 25 f4 96 04 08        jmp    *0x80496f4
 80483be:    68 18 00 00 00           push   $0x18
 80483c3:    e9 b0 ff ff ff           jmp    8048378 <_init+0x30>
Disassembly of section .text:

080483d0 <_start>:
 80483d0:    31 ed                    xor    %ebp,%ebp
 80483d2:    5e                       pop    %esi
 80483d3:    89 e1                    mov    %esp,%ecx
 80483d5:    83 e4 f0                 and    $0xfffffff0,%esp
 80483d8:    50                       push   %eax
 80483d9:    54                       push   %esp
 80483da:    52                       push   %edx
 80483db:    68 a0 84 04 08           push   $0x80484a0
 80483e0:    68 b0 84 04 08           push   $0x80484b0
 80483e5:    51                       push   %ecx
 80483e6:    56                       push   %esi
 80483e7:    68 54 84 04 08           push   $0x8048454
 80483ec:    e8 a7 ff ff ff           call   8048398 <__libc_start_main@plt>
 80483f1:    f4                       hlt   
 80483f2:    90                       nop   
 80483f3:    90                       nop   
 80483f4:    90                       nop   
 80483f5:    90                       nop   
 80483f6:    90                       nop   
 80483f7:    90                       nop   
 80483f8:    90                       nop   
 80483f9:    90                       nop   
 80483fa:    90                       nop   
 80483fb:    90                       nop   
 80483fc:    90                       nop   
 80483fd:    90                       nop   
 80483fe:    90                       nop   
 80483ff:    90                       nop   

08048400 <__do_global_dtors_aux>:
 8048400:    55                       push   %ebp
 8048401:    89 e5                    mov    %esp,%ebp
 8048403:    83 ec 08                 sub    $0x8,%esp
 8048406:    80 3d 04 97 04 08 00     cmpb   $0x0,0x8049704
 804840d:    74 0c                    je     804841b <__do_global_dtors_aux+0x1b>
 804840f:    eb 1c                    jmp    804842d <__do_global_dtors_aux+0x2d>
 8048411:    83 c0 04                 add    $0x4,%eax
 8048414:    a3 00 97 04 08           mov    %eax,0x8049700
 8048419:    ff d2                    call   *%edx
 804841b:    a1 00 97 04 08           mov    0x8049700,%eax
 8048420:    8b 10                    mov    (%eax),%edx
 8048422:    85 d2                    test   %edx,%edx
 8048424:    75 eb                    jne    8048411 <__do_global_dtors_aux+0x11>
 8048426:    c6 05 04 97 04 08 01     movb   $0x1,0x8049704
 804842d:    c9                       leave 
 804842e:    c3                       ret   
 804842f:    90                       nop   

08048430 <frame_dummy>:
 8048430:    55                       push   %ebp
 8048431:    89 e5                    mov    %esp,%ebp
 8048433:    83 ec 08                 sub    $0x8,%esp
 8048436:    a1 ec 95 04 08           mov    0x80495ec,%eax
 804843b:    85 c0                    test   %eax,%eax
 804843d:    74 12                    je     8048451 <frame_dummy+0x21>
 804843f:    b8 00 00 00 00           mov    $0x0,%eax
 8048444:    85 c0                    test   %eax,%eax
 8048446:    74 09                    je     8048451 <frame_dummy+0x21>
 8048448:    c7 04 24 ec 95 04 08     movl   $0x80495ec,(%esp)
 804844f:    ff d0                    call   *%eax
 8048451:    c9                       leave 
 8048452:    c3                       ret   
 8048453:    90                       nop   

08048454 <main>:
#include <stdio.h>

int main()
 8048454:    8d 4c 24 04              lea    0x4(%esp),%ecx
 8048458:    83 e4 f0                 and    $0xfffffff0,%esp
 804845b:    ff 71 fc                 pushl  -0x4(%ecx)
 804845e:    55                       push   %ebp
 804845f:    89 e5                    mov    %esp,%ebp
 8048461:    51                       push   %ecx
 8048462:    83 ec 14                 sub    $0x14,%esp
{
    int i = 0;
 8048465:    c7 45 f8 00 00 00 00     movl   $0x0,-0x8(%ebp)

    for (i = 0; i < 5; i ++ )
 804846c:    c7 45 f8 00 00 00 00     movl   $0x0,-0x8(%ebp)
 8048473:    eb 10                    jmp    8048485 <main+0x31>
    {
        printf("Hello World\n");
 8048475:    c7 04 24 60 85 04 08     movl   $0x8048560,(%esp)
 804847c:    e8 27 ff ff ff           call   80483a8 <puts@plt>

int main()
{
    int i = 0;

    for (i = 0; i < 5; i ++ )
 8048481:    83 45 f8 01              addl   $0x1,-0x8(%ebp)
 8048485:    83 7d f8 04              cmpl   $0x4,-0x8(%ebp)
 8048489:    7e ea                    jle    8048475 <main+0x21>
    {
        printf("Hello World\n");
    }

    return 0;
 804848b:    b8 00 00 00 00           mov    $0x0,%eax
}
 8048490:    83 c4 14                 add    $0x14,%esp
 8048493:    59                       pop    %ecx
 8048494:    5d                       pop    %ebp
 8048495:    8d 61 fc                 lea    -0x4(%ecx),%esp
 8048498:    c3                       ret   
 8048499:    90                       nop   
 804849a:    90                       nop   
 804849b:    90                       nop   
 804849c:    90                       nop   
 804849d:    90                       nop   
 804849e:    90                       nop   
 804849f:    90                       nop   

080484a0 <__libc_csu_fini>:
 80484a0:    55                       push   %ebp
 80484a1:    89 e5                    mov    %esp,%ebp
 80484a3:    5d                       pop    %ebp
 80484a4:    c3                       ret   
 80484a5:    8d 74 26 00              lea    0x0(%esi),%esi
 80484a9:    8d bc 27 00 00 00 00     lea    0x0(%edi),%edi

080484b0 <__libc_csu_init>:
 80484b0:    55                       push   %ebp
 80484b1:    89 e5                    mov    %esp,%ebp
 80484b3:    57                       push   %edi
 80484b4:    56                       push   %esi
 80484b5:    53                       push   %ebx
 80484b6:    e8 4f 00 00 00           call   804850a <__i686.get_pc_thunk.bx>
 80484bb:    81 c3 21 12 00 00        add    $0x1221,%ebx
 80484c1:    83 ec 0c                 sub    $0xc,%esp
 80484c4:    e8 7f fe ff ff           call   8048348 <_init>
 80484c9:    8d bb 00 ff ff ff        lea    -0x100(%ebx),%edi
 80484cf:    8d 83 00 ff ff ff        lea    -0x100(%ebx),%eax
 80484d5:    29 c7                    sub    %eax,%edi
 80484d7:    c1 ff 02                 sar    $0x2,%edi
 80484da:    85 ff                    test   %edi,%edi
 80484dc:    74 24                    je     8048502 <__libc_csu_init+0x52>
 80484de:    31 f6                    xor    %esi,%esi
 80484e0:    8b 45 10                 mov    0x10(%ebp),%eax
 80484e3:    89 44 24 08              mov    %eax,0x8(%esp)
 80484e7:    8b 45 0c                 mov    0xc(%ebp),%eax
 80484ea:    89 44 24 04              mov    %eax,0x4(%esp)
 80484ee:    8b 45 08                 mov    0x8(%ebp),%eax
 80484f1:    89 04 24                 mov    %eax,(%esp)
 80484f4:    ff 94 b3 00 ff ff ff     call   *-0x100(%ebx,%esi,4)
 80484fb:    83 c6 01                 add    $0x1,%esi
 80484fe:    39 f7                    cmp    %esi,%edi
 8048500:    75 de                    jne    80484e0 <__libc_csu_init+0x30>
 8048502:    83 c4 0c                 add    $0xc,%esp
 8048505:    5b                       pop    %ebx
 8048506:    5e                       pop    %esi
 8048507:    5f                       pop    %edi
 8048508:    5d                       pop    %ebp
 8048509:    c3                       ret   

0804850a <__i686.get_pc_thunk.bx>:
 804850a:    8b 1c 24                 mov    (%esp),%ebx
 804850d:    c3                       ret   
 804850e:    90                       nop   
 804850f:    90                       nop   

08048510 <__do_global_ctors_aux>:
 8048510:    55                       push   %ebp
 8048511:    89 e5                    mov    %esp,%ebp
 8048513:    53                       push   %ebx
 8048514:    83 ec 04                 sub    $0x4,%esp
 8048517:    a1 dc 95 04 08           mov    0x80495dc,%eax
 804851c:    83 f8 ff                 cmp    $0xffffffff,%eax
 804851f:    74 12                    je     8048533 <__do_global_ctors_aux+0x23>
 8048521:    31 db                    xor    %ebx,%ebx
 8048523:    ff d0                    call   *%eax
 8048525:    8b 83 d8 95 04 08        mov    0x80495d8(%ebx),%eax
 804852b:    83 eb 04                 sub    $0x4,%ebx
 804852e:    83 f8 ff                 cmp    $0xffffffff,%eax
 8048531:    75 f0                    jne    8048523 <__do_global_ctors_aux+0x13>
 8048533:    83 c4 04                 add    $0x4,%esp
 8048536:    5b                       pop    %ebx
 8048537:    5d                       pop    %ebp
 8048538:    c3                       ret   
 8048539:    90                       nop   
 804853a:    90                       nop   
 804853b:    90                       nop   
Disassembly of section .fini:

0804853c <_fini>:
 804853c:    55                       push   %ebp
 804853d:    89 e5                    mov    %esp,%ebp
 804853f:    53                       push   %ebx
 8048540:    83 ec 04                 sub    $0x4,%esp
 8048543:    e8 00 00 00 00           call   8048548 <_fini+0xc>
 8048548:    5b                       pop    %ebx
 8048549:    81 c3 94 11 00 00        add    $0x1194,%ebx
 804854f:    e8 ac fe ff ff           call   8048400 <__do_global_dtors_aux>
 8048554:    59                       pop    %ecx
 8048555:    5b                       pop    %ebx
 8048556:    c9                       leave 
 8048557:    c3                       ret   



Reference
  • http://24alpha.wordpress.com/2007/12/18/how-to-get-gcc-to-interleave-assembly-output-with-original-source-code/


저작자 표시 비영리 동일 조건 변경 허락