'팁&테크/C & C++'에 해당되는 글 9건

  1. 2017.01.26 C++ 메모
  2. 2016.07.26 GCC 4.7 이상 설치 및 사용방법
  3. 2016.01.08 boost 컴파일 옵션
  4. 2015.09.18 Boost 관련 메모
  5. 2015.03.25 리눅스 core dump 파일 오류 확인 방법 2
  6. 2015.01.08 FireBreath 관련 메모
  7. 2014.10.22 json 용 escape 처리
  8. 2009.01.14 string.h 함수 간단 정리(정리중)
  9. 2009.01.14 ctype.h 함수 간단 정리 (정리중)
2017. 1. 26. 09:39

C++ 메모

1. vector 순환하며 삭제


auto i = std::begin(inv);
while (i != std::end(inv)) {
    // Do some stuff
    if (blah)
        i = inv.erase(i);
    else
        ++i;
}
for (auto it = begin(vec); it != end(vec);) {
  if (*it > 5)
    it = vec.erase(it);
  else
    ++it;
}


2. 맵 순환하며 삭제

std::map::iterator itr = myMap.begin();
while (itr != myMap.end()) {
    if (ShouldDelete(*itr)) {
       std::map::iterator toErase = itr;
       ++itr;
       myMap.erase(toErase);
    } else {
       ++itr;
    }
}


3.

2016. 7. 26. 13:24

GCC 4.7 이상 설치 및 사용방법

GCC 4.7 버전 부터 C++11 을 정식 지원함.


GCC 4.7 설치

wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo -O /etc/yum.repos.d/devtools-1.1.repo
yum install devtoolset-1.1-gcc devtoolset-1.1-gcc-c++ devtoolset-1.1-binutils


GCC 4.7을 기본으로 적용하는 명령

scl enable devtoolset-1.1 bash


------------------------


GCC 4.8 설치

wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo
yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++


GCC 4.8을 기본으로 적용하는 명령

scl enable devtoolset-2 bash


--------------------------


.bashrc 나 .bash_profile 에 아래처럼 enable 스크립트를 적용하도록 하면 됨

source /opt/centos/devtoolset-1.1/enable

아니면

source scl_source enable devtoolset-1.1

scl 없이는 아래와 같이 경로를 .bash_profile등에 지정한 다음에 $CC 등과 같이 이용

export CC=/opt/centos/devtoolset-1.1/root/usr/bin/gcc
export CPP=/opt/centos/devtoolset-1.1/root/usr/bin/cpp
export CXX=/opt/centos/devtoolset-1.1/root/usr/bin/c++



2016. 1. 8. 13:49

boost 컴파일 옵션

설치방법


1. 사용하려는 Visual Studio 의 도구명령 프롬프트(CMD같은거)를 연다.


2. Boost 라이브러리를 압축해제한 경로로 이동한다.


3. bootstarp.bat(또는 sh)를 실행하여 bjam 파일을 만든다.

   여기서 cl.exe 파일을 못찻는다는 오류가 나면 도구명령 프롬프트로 연게 아니니 다시 한번 확인하거나.

   cl.exe 가 있는 경로(보통 "Visual Studio 설치경로\bin")를 path 에추가 해도 된다.


4. bjam 으로 필요한 dll,lib 파일을 생성하면 된다.

   아래는 생성옵션 정리


-toolset=msvc-10.0

: Visual Studio 2010(msvc-10.0), 2012 (msvc-11.0), 2013(msvc-12.0) 을 이용해 컴파일함을 알려준다. 다른 버전을 사용할 경우에는 그에 맞는 버전을 지정하면 된다.


-architecture=x86

: 아키텍쳐를 지정한다. x86이나, x64라면 디폴트 값으로 x86으로 지정되고 IA64의 경우에는 –architecture=ia64


address-model=64

: 주소 모델을 지정한다. x64로 빌드할것이므로 64 를 지정한다. 지정안한다면 32가 디폴트 값으로 사용된다.


link=static

: 어떤 형태로 빌드할 것인지 나타낸다. static(lib), shared(dll) 두가지 옵션이 있다. link=static,shared 로 둘다 지정도 가능


runtime-link=static,shared

: 사용하는 C – Runtime Library 를 어떻게 연결할 것인지를 나타낸다. 나는 둘 다 지정했다.


variant=release

: 빌드된 라이브러리가 릴리즈용인지, 디버그 용인지를 나타낸다. variant=release,debug 와 같이 지정도 가능.


–without=mpi


–without=python


: 부스트 라이브러리는 사용을 위해 또 다른 라이브러리가 이미 설치 되 있어야 하는 경우도 있다. MPI 가 그 경우.

나는 MPI와 파이썬을 사용하지 않으므로 둘 다 빌드하지 않겠다는 옵션을 주었다.


–stagedir=stage64_lib_release

: 빌드된 라이브러리들이 위치 할 폴더. %BOOSTROOT% \ %STAGEDIR% 에 생성된다.


stage

: Stage 옵션. 헤더파일을 제외하고 라이브러리만 생성한다.


-j 2


: 쓰레드를 이용해 동시에 몇개의 작업을 할 것인가, 이경우는 두개의 작업을 동시에 하겠다고 옵션을 주었다.


-------------------------------------------------------------------

생성된 파일명의 의미


boost

: 이 파일이 link=shared 옵션으로 생성된 DLL 파일임을 나타낸다. LIB 파일이라면 libboost 로 시작한다.


libboost

: 이 파일이 link=static 옵션으로 생성된 LIB 파일임을 나타낸다.


data_time

: 라이브러리 이름을 나타낸다


vc100

: 컴파일하는데 사용된 툴과 버전을 나타낸다. vc 10.0 버전을 사용하였다.


mt

: 이 멀티 쓰레드용 부스트 라이브러리임을 나타낸다.


s

: runtime-link=static 옵션으로 C - Runtime Library 가 static 으로 링크 되었음을 알려준다.


g

: variant:debug 옵션으로 C - Runtime Library 가 디버그 버전으로 링크 되었음을 알려준다.


d

: variant:debug 옵션으로 이 부스트 라이브러리가 디버거 버전으로 컴파일 되었음을 알려준다.


1_46_1

: 부스트 라이브러리의 버전을 알려줌




libboost_mt 

: 멀티 쓰레드용 릴리즈 버전, C Runtime Library 는 shared 방식으로 링크됨. 비주얼 스튜디오 /MT 옵션용 라이브러리


libboost_mt_gd

: 멀티 쓰레드용 디버그 버전, C Runtime Library 는 shared 방식으로 링크됨. 비주얼 스튜디오 /MTd 옵션용 라이브러리


libboost_mt_s

: 멀티 쓰레드용 릴리즈 버전, C Runtime Library 는 static 방식으로 링크됨. 비주얼 스튜디오 /MD 옵션용 라이브러리


libboost_mt_sgd

: 멀티 쓰레드용 디버그 버전, C Runtime Library 는 static 방식으로 링크됨. 비주얼 스튜디오 /MDd 옵션용 라이브러리


-------------------------------------------------------------------

일반 용도 : (/MD 옵션)

mkdir build\amd64\v110

bjam --toolset=msvc-11.0 architecture=x86 address-model=64 link=static runtime-link=static variant=release,debug --without=mpi --without=python --stagedir=build/amd64/v110 stage -j 2

mkdir build\x86\v110

bjam --toolset=msvc-11.0 architecture=x86 address-model=32 link=static runtime-link=static variant=release,debug --without=mpi --without=python --stagedir=build/x86/v110 stage -j 2


mkdir build\amd64\v100

bjam --toolset=msvc-10.0 architecture=x86 address-model=64 link=static runtime-link=static variant=release,debug --without=mpi --without=python --stagedir=build/amd64/v100 stage -j 2

mkdir build\x86\v100

bjam --toolset=msvc-10.0 architecture=x86 address-model=32 link=static runtime-link=static variant=release,debug --without=mpi --without=python --stagedir=build/x86/v100 stage -j 2


Matlab 용도 : (/MT 옵션을 사용해야한다?)

mkdir build_mt\amd64\v100

bjam --toolset=msvc-10.0 architecture=x86 address-model=64 link=static runtime-link=shared variant=release,debug --without=mpi --without=python --stagedir=build_mt/amd64/v100 stage -j 2

mkdir build_mt\x86\v100

bjam --toolset=msvc-10.0 architecture=x86 address-model=32 link=static runtime-link=shared variant=release,debug --without=mpi --without=python --stagedir=build_mt/x86/v100 stage -j 2




toolset 설정 시 아래와 같은 오류가 발생할 경우.

error: msvc initialization: parameter 'version' inconsistent

error: no value was specified in earlier initialization

error: an explicit value is specified now


project-config.jam 파일을 열어서 아래 부분을 사용할 toolset 버전으로 변경해준다.

using msvc;  -> using msvc : 11.0;



2015. 9. 18. 17:55

Boost 관련 메모

1.boost 를 사용할때 class boost::system::error_category 링크 오류가 나는 경우


1>Timer.obj : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAABVerror_category@12@XZ)

1>UdpBinder.obj : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAABVerror_category@12@XZ)

1>Service.obj : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAABVerror_category@12@XZ)


부스트 라이브러리를 사용해서 컴파일 하는데 위와같은 링크오류가 계속 나서 찾아본 결과


프로젝트의 타켓 플랫폼(32bit x86, 64bit x64)과 부스트 라이브러리가 컴파일된 address-model(32, 64)이 다를 경우 발생하는 오류.


boost를 컴파일 할때 address-model=32 로 했을 경우 타겟 플랫폼은 32bit(x86)이어야 되고

address-model=64는 프로젝트 타겟 64bit(x64) 플랫폼으로 사용되어야 하니 주의할 것.


또 boost는 address-model을 지정해주지 않으면 32bit(x86)으로 컴파일 되니 이것도 주의.


2. C++11 reverse range based for loop (for문 역순)


std::list x { 2, 3, 5, 7, 11, 13, 17, 19 };
    for (auto i : boost::adaptors::reverse(x))
        std::cout << i << '\n';


마지막에 </int>붙는건 티스토리 에디터 버그같으네요.

2015. 3. 25. 16:24

리눅스 core dump 파일 오류 확인 방법

1. core dump 파일을 남기는지 확인

ulimit -a 명령 실행

core file size 항목이 설정되 있는 경우 가능

없다면 ulimit -c unlimited 명령으로 설정


그리고 /etc/security/limits.conf 에 아래 내용을 추가해야 재부팅시도 유지가 된다.

*               -       core            unlimited


2. file 명령으로 어느 파일의 core dump 인지 확인

예) file core.26325

core.26325: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from './frontend'


3. GDB를 사용하여 코어파일 분석

예) gdb ./frontend ./core.26325


명령설명

backtrace : 죽기직전 호출되었던 호출스택 확인



break Abnomal : 마지막 호출 함수에 break point 설정, 그 이후 r 키로 한줄씩 디버깅



---------------------------- 추가 --------------------------------------


출처 : http://cafe.naver.com/nexcore.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=43



1.1.       Core Dump 파일 분석하기

1.1.1.   코어  덤프  파일  정보  보기 

 

GDB로 코어 덤프 파일 분석하기 , gdb [프로그램명] [코어파일명]

 

gdb cursorserv core.9122

HP gdb 5.8 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x.

Copyright 1986 - 2001 Free Software Foundation, Inc.

. . .

 

backtrace 명령어로 콜스택 backtrace

(gdb) bt

#0  inline std::allocator<char>::allocator(std::allocator<char> const&) ()

    at /opt/aCC/include_std/memory:252

#1  0x9fffffffbb8b7440:0 in inline std::basic_string<char,std::char_traits<char>,std::allocator<char> >::get_allocator() const ()

    at /opt/aCC/include_std/string:774

#2  0x9fffffffbb8b7420:0 in std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string (this=0x9fffffffffffdb58, __s=@0x0)

    at /opt/aCC/include_std/string:1035

#3  0x9fffffffbbbb2100:0 in nexcore::sql::Record::setValue (

    this=0x9fffffffffffdd30, key=@0x0, value=@0x9fffffffffffdca8)

    at nexcore/sql/Record.cpp:67

#4  0x9fffffffb99ec310:0 in int nexcore::sql::SqlManager::select<TestFun*,bool

    (this=0x600000000006d0c0, statementId=@0x9fffffffffffde00,

    params=0x9fffffffffffde30, c=0x60000000001340b0, mf=(bool ( class TestFun

    ::*)(class nexcore::sql::Record *...)) -147599808)

    at /home/jsh/nexbuild/nana/include/nexcore/sql/SqlManager.hpp:157

#5  0x9fffffffb99e9240:0 in TestFun::perform (this=0x60000000001340b0,

    request=0x6000000000141950, response=0x6000000000025840) at TestFun.cpp:103

#6  0x9fffffffbbc74510:2 in inline std::allocator<char>::allocator() ()

    at /opt/aCC/include_std/memory:250

 

의심되는 스택 프레임으로 이동한다. 예를 들어 4번 프레임을 조사하고 싶으면, frame 4를 입력한다.

 

Frame 4를 선택해서 스택 정보 보기

(gdb) f 4

#4  0x9fffffffb99ec310:0 in int nexcore::sql::SqlManager::select<TestFun*,bool

    (this=0x600000000006d0c0, statementId=@0x9fffffffffffde00,

    params=0x9fffffffffffde30, c=0x60000000001340b0, mf=(bool ( class TestFun

    ::*)(class nexcore::sql::Record *...)) -147599808)

    at /home/jsh/nexbuild/nana/include/nexcore/sql/SqlManager.hpp:157

157                                                             record.setValue( colNames[i], rset->getString(i+1) );

 

해당 스택의 소스 보기

(gdb) list

152                                             while(rset->next())

153                                             {

154                                                     Record record;

155                                                     for (int i=0; i<colCount; i++)

156                                                     {

157                                                             record.setValue( colNames[i], rset->getString(i+1) );

158                                                     }

159                                                    

160                                                     // call callback function

 

해당 스택의 argument 보기

(gdb) info arg

this = (class nexcore::sql::SqlManager * const) 0x600000000006d0c0

statementId = (

    class std::basic_string<char, std::char_traits<char>, std::allocator<char>>

     &) @0x9fffffffffffde00: {_C_data = 0x600000000013a4f0 "select",

  static __nullref = <optimized out>, static npos = <optimized out>}

params = (class nexcore::sql::Params *) 0x9fffffffffffde30

c = (class TestFun *) 0x60000000001340b0

mf = (bool ( class TestFun::*)(class nexcore::sql::Record *...)) (bool (

    class TestFun::*)(class nexcore::sql::Record *...)) -147599808

 

해당 스택의 local value 보기

(gdb) info local

i = 0

record = {record = {__t = {_C_buffer_list = 0x6000000000134d40,

      _C_free_list = 0x0, _C_next_avail = 0x60000000003cc650,

      _C_last = 0x60000000003ccc20, _C_header = 0x60000000003cc620,

      _C_node_count = 0, _C_insert_always = false,

      _C_key_compare = {<std::binary_function<std::string, std::string, bool>> = {<No data fields>}, <No data fields>}}}, __vfp = 0x9fffffffbb784110}

colCount = 2

resultCount = 0

selectCnt = 0

query = {

  _C_data = 0x6000000000033830 " SELECT A.NATION_CD AS nationCD, A.NATION_NM AS nationNM FROM PI_NATION A WHERE A.NATION_CD LIKE '%' || #nationCD# || '%' AND A.DEL_FLG='N' ", static __nullref = <optimized out>,

  static npos = <optimized out>}

 

위와 같은 방식으로 코어 덤프 파일로부터 콜스택을 추적하여 프로그램이 비정상 종료된 원인을 찾아낸다. 자세한 명령어 및 검사방법은 GDB 기본 명령어 참조한다.


추가 : local variable 이외에 해당 클래스의 멤버변수를 확인할 경우 print 또는 p를 사용한다.

(gdb) p 변수명   -> 일반 변수

(gdb) p* 변수명  -> 포인터형 변수(new등)


1.2.       실행중인 프로세스 디버깅

 

실행중인 프로세스 PID 보기 , ps –ef | grep [프로세스 명]

 

SKA1PAP1:/>ps -ef | grep ae001serv

muxplt1 28238     1  0 12:00:43 ?         0:00 ae001serv -g 1 -i 200 -u SKA1PAP1 -U  muxplt1  6153  6114  0 12:39:04 pts/10    0:00 grep ae001serv

 

 

실행중인 프로세스에 attach , gdb [프로세스 명] [pid]

 

SKA1PAP1:/ >gdb ae001serv 28238

HP gdb 5.8 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x.

Copyright 1986 - 2001 Free Software Foundation, Inc.

. . .

 

현재 프로세스의 스택 정보

(gdb) where

#0  0x9fffffffe2e31ad0:0 in _msgrcv_sys+0x30 () from /lib/hpux64/libc.so.1

#1  0x9fffffffe2e41270:0 in msgrcv ()

    at ../../../../../core/libs/libc/shared_em_64_perf/../core/syscalls/t_msgrcv.c:19

#2  0x9fffffffe43ccfe0:0 in _tmmbrecvm () at msg/tmmbrecvm.c:364

#3  0x9fffffffe4195a10:0 in _tmmsgrcv () at tmgetrply.c:652


2015. 1. 8. 10:35

FireBreath 관련 메모

사용자 권한 관련으로 실행에 문제가 날 경우(기본 per-user 를 per-machine 로 변경하는 법)

PluginConfig.cmake 파일에 #set (FB_ATLREG_MACHINEWIDE 1) 에 주석 해제

2014. 10. 22. 10:35

json 용 escape 처리

json 으로 문자열을 내려보낼때 개행 같은 문자가 문자열에 포함된 경우

내용자체가 개행되서 처리될 경우 변경하는 방법


------- string 을 바로 변경하는 경우

#include <string>

#include <sstream>


std::string escapeJsonString(const std::string& input) {

    std::ostringstream ss;

    for (auto iter = input.cbegin(); iter != input.cend(); iter++) {

    //C++98/03:

    //for (std::string::const_iterator iter = input.begin(); iter != input.end(); iter++) {

        switch (*iter) {

            case '\\': ss << "\\\\"; break;

            case '"': ss << "\\\""; break;

            case '/': ss << "\\/"; break;

            case '\b': ss << "\\b"; break;

            case '\f': ss << "\\f"; break;

            case '\n': ss << "\\n"; break;

            case '\r': ss << "\\r"; break;

            case '\t': ss << "\\t"; break;

            default: ss << *iter; break;

        }

    }

    return ss.str();

}


--------- ascii char 형태인 경우 (변수는 알아서 적용할것)

for(_UInt32 i = 0; i < node->m_Value->m_Value.length(); ++i)

{

    if(node->m_Value->m_Value[i] == 34) //"

    {

        str += "\\\"";

    }

    else if(node->m_Value->m_Value[i] == 92)

    {

        str += "\\\\";

    }

    else if(node->m_Value->m_Value[i] == 13)

    {

        //str += "\\r";

    }

    else if(node->m_Value->m_Value[i] == 10)

    {

        str += "\\n";

    }

    else if(node->m_Value->m_Value[i] == 8)

    {

        str += "\\b";

    }

    else if(node->m_Value->m_Value[i] == 9)

    {

        str += "\\t";

    }

    else

        str += node->m_Value->m_Value[i];

}

2009. 1. 14. 15:50

string.h 함수 간단 정리(정리중)

char * strcasecmp( const char *s1, const char *s2, size_t n)
문자열 비교(대소문자 무시)
0 이면 문자열 같음
0 < 결과 값이면 s1 > s2
0 > 결과 값이면 s1 < s2

char * strncasecmp( const char *s1, const char *s2, size_t n)
지정 길이만큼 문자열 비교(대소문자 무시)
0 이면 문자열 같음
0 < 결과 값이면 s1 > s2
0 > 결과 값이면 s1 < s2

char * strcpy( char *dest, const char *src)
문자열 복사(src를 dest로)
복사한 문자열 반환

char * strncpy( char *dest, const char *src, size_t n)
문자열을 길이만큼 복사(src를 dest로)
복사한 문자열 반환

char * strdup( const char *str)
문자열을 변수가 아닌 메모리에 복사
메모리의 주소(포인터) 반환

size_t strlen( const char *str)
문자열의 길이를 반환
byte단위 길이 반환

char *strchr( const char *str, int chr)
문자열에서 첫번째 문자 위치 반환
발견한 첫번째 문자의 포인터주소 반환
문자가 없을 경우 NULL 반환


char *strrchr( const char *str, int chr)
문자열에서 마지막 문자 위치 반환
발견한 마지막 문자의 포인터주소 반환
문자가 없을 경우 NULL 반환


char *strstr( const char *str1, const char *str2)
자열에서 검색할 문자의 위치 반환
발견한 마지막 문자열의 포인터주소 반환
문자가 없을 경우 NULL 반환
size_t strspn(const char *s1, const char *s2)
문자열 s1이 s2의 문자들로 이루어질때 까지의 길이를 반환
strspn("abcde","abzx") -> 2를 반환

size_t strcspn(const char *s1, const char *s2)
문자열 s1이 s2의 문자들로 이루어지지 않을때 까지의 길이를 반환
strcspn("abcde","defg") -> 3을 반환

char *strpbrk(const char *s1, const char *s2)
문자열 s1이 s2의 문자들로 시작되는 위치를 반환
strpbrk("abcde","cdef") -> c 의 위치에 해당하는 포인터 주소 반환("cde" 반환)

char *strtok(char *restrict s1, const char *restrict s2)
s1을 s2로 나눈 문자열을 얻으려고 하때 사용
strtok("aaa.bbb.ccc",".") -> aaa 반환
strtok(NULL,".") -> 나머지 문자열을 반환
반환할 문자열이 없으면 NULL 반환

2009. 1. 14. 15:46

ctype.h 함수 간단 정리 (정리중)

int isalnum( int c) 
문자가 알파벳 또는 숫자인지 판별 
0아닌값(참) : 0(거짓)

int isalpha( int c)
문자가 알파벳 인지 판별 
0아닌값(참) : 0(거짓)

int isascii( int c) 
아스키 문자인지 판별 
0아닌값(참) : 0(거짓)

int isblank( int c) 
공백 또는 탭문자인지 판별 
0아닌값(참) : 0(거짓)

int iscntrl( int c) 
제어문자인지 판별 
0아닌값(참) : 0(거짓)

int isdigit( int c) 
숫자형 문자인지 판별 
0아닌값(참) : 0(거짓)

int isgraph( int c) 
인쇄 가능한 문자인지 판별 
0아닌값(참) : 0(거짓) - 공백문자는 거짓

int islower( int c) 
소문자 인지 판별 
0아닌값(참) : 0(거짓)

int isprint( int c) 
인쇄 가능한 문자인지 판별 
0아닌값(참) : 0(거짓) - 공백문자는 참

int ispunct( int c) 
구획 문자인지 판별 
0아닌값(참) : 0(거짓)

int isspace( int c) 
공백문자(탭,개행등 포함)인지 판별 
0아닌값(참) : 0(거짓)

int isspace( int c) 
대문자 인지 판별 
0아닌값(참) : 0(거짓)

int isxdigit( int c) 
16진수형 문자인지 판별 
0아닌값(참) : 0(거짓)

int toupper( int ch) 
소문자를 대문자로 반환 
소문자만 대문자로 반환, 그외에는 그대로 반환

int tolower( int ch) 
대문자를 소문자로 반환 
대문자만 소문자로 반환, 그외에는 그대로 반환