IT story

GCC로 미리 컴파일 된 헤더

hot-time 2020. 9. 5. 10:37
반응형

GCC로 미리 컴파일 된 헤더


누구든지 GCC로 작업하는 미리 컴파일 된 헤더를 얻는 데 성공 했습니까? 나는 내 시도에 운이 없었고 그것을 설정하는 방법에 대한 많은 좋은 예를 보지 못했습니다. cygwin gcc 3.4.4에서 시도하고 Ubuntu에서 4.0을 사용했습니다.


나는 확실히 성공했습니다. 먼저 다음 코드를 사용했습니다.


#include <boost/xpressive/xpressive.hpp>
#include <iostream>

using namespace std;
using namespace boost::xpressive;

//A simple regex test
int main()
{
    std::string hello( "hello world!" );

    sregex rex = sregex::compile( "(\\w+) (\\w+)!" );
    smatch what;

    if( regex_match( hello, what, rex ) )
    {
        std::cout << what[0] << '\n'; // whole match
        std::cout << what[1] << '\n'; // first capture
        std::cout << what[2] << '\n'; // second capture
    }
    return 0;
}

이것은 Boost Xpressive의 안녕하세요 세계였습니다 (링크는 아래 참조). 먼저 -Hgcc 옵션으로 컴파일했습니다 . 그것은 그것이 사용한 엄청난 헤더 목록을 보여주었습니다. 그런 다음 IDE (code :: blocks)가 생성하는 컴파일 플래그를 살펴 보았고 다음과 같은 것을 보았습니다.

g++ -Wall -fexceptions -g -c main.cpp -o obj/Debug/main.o

그래서 정확히 동일한 플래그로 Xpressive.hpp 파일을 컴파일하는 명령을 작성했습니다.

sudo g++ -Wall -fexceptions -g /usr/local/include/boost/xpressive/xpressive.hpp

를 사용하여 원래 코드를 다시 컴파일하고 -H다음 출력을 얻었습니다.

g ++-벽 -f 예외 -H -g -c main.cpp -o obj / Debug / main.o
! /usr/local/include/boost/xpressive/xpressive.hpp.gch
main.cpp
. /usr/include/c++/4.4/iostream
.. /usr/include/c++/4.4/x86_64-linux-gnu/bits/c++config.h
.. /usr/include/c++/4.4/ostream
.. /usr/include/c++/4.4/istream
main.cpp

! 컴파일러가 미리 컴파일 된 헤더를 사용할 수 있음을 의미합니다. x는 사용할 수 없음을 의미합니다. 적절한 컴파일러 플래그를 사용하는 것이 중요합니다. -H를 벗고 속도 테스트를 실행했습니다. 미리 컴파일 된 헤더는 14 초에서 11 초로 향상되었습니다. 나쁘지는 않지만 좋지는 않습니다.

참고 : 여기 예제에 대한 링크는 다음과 같습니다 http://www.boost.org/doc/libs/1_43_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.examples 나는 직장에 가져올 수 없습니다 게시하다.

BTW : 다음 g ++를 사용하고 있습니다.

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3


먼저 여기에서 설명서를 참조하십시오 .

다른 파일과 마찬가지로 헤더를 컴파일하지만 접미사가 붙은 파일 안에 출력을 넣습니다. .gch.

예를 들어 stdafx.h를 미리 컴파일 stdafx.h.gch하면 다음을 포함 할 때마다 자동으로 검색되는 미리 컴파일 된 헤더가 있습니다.stdafx.h

예:

stdafx.h :

#include <string>
#include <stdio.h>

a.cpp :

#include "stdafx.h"
int main(int argc, char**argv)
{
  std::string s = "Hi";
  return 0;
}

그런 다음 다음과 같이 컴파일하십시오.

> g++ -c stdafx.h -o stdafx.h.gch
> g++ a.cpp
> ./a.out

1 단계 후에 stdafx.h를 제거해도 컴파일이 작동합니다.


-xC ++에 대한 지정 헤더가 미리 컴파일 -x c++-header하지 -x c++. PCH 사용 예는 다음과 같습니다.

pch.h:

// Put your common include files here: Boost, STL as well as your project's headers.

main.cpp:

#include "pch.h"
// Use the PCH here.

다음과 같이 PCH를 생성합니다.

$ g++ -x c++-header -o pch.h.gch -c pch.h

사용 pch.h.gch하려면은와 동일한 디렉토리에 있어야하므로이 디렉토리 pch.h에서 위 명령을 실행해야합니다 pch.h.


I have managed to get precompiled headers working under gcc once in the past, and I recall having problems then as well. The thing to remember is that gcc will ignore the file (header.h.gch or similar) if certain conditions are not met, a list of which can be found on the gcc precompiled header documentation page.

Generally it's safest to have your build system compile the .gch file as a first step, with the same command line options and executable as the rest of your source. This ensures the file is up to date and that there are no subtle differences.

It's probably also a good idea to get it working with a contrived example first, just to remove the possibility that your problems are specific to source code in your project.


Call gcc same way as you call it for your source file but with a header file.

e.g.

g++ $(CPPFLAGS) test.h

this generates a file called test.h.gch

Every time gcc searches for test.h it look first for test.h.gch and if it finds it it uses it automatically.

More information can be found under GCC Precompiled Headers

참고URL : https://stackoverflow.com/questions/58841/precompiled-headers-with-gcc

반응형