2017-10-01 27 views
0

this example 다음에 MinGW-w64를 따르는 정적 라이브러리를 실행 파일과 연결하려고합니다.링크 타임 최적화로 정의되지 않은 참조

내 CMakeLists 파일 : (이 그 대답의 하나의 동일 점에 유의)

cmake_minimum_required (VERSION 2.6) 
project (hellow) 
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto") 
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto") 
SET(CMAKE_AR "gcc-ar") 
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>") 
SET(CMAKE_C_ARCHIVE_FINISH true) 
add_library(hello STATIC libhello.c) 
add_executable(hellow hello.c) 
target_link_libraries(hellow hello) 
add_dependencies(hellow hello) 

에서는 hello.c :

extern void hello(void); 

int main(void) { 
    hello(); 
    return 0; 
} 

libhello.c :

#include <stdio.h> 

void hello(void) { 
    puts("Hello"); 
} 

구성이 예상대로 작동합니다.

01 23,592,988,157,

있는 그대로, 그러나 다음과 같은 오류가 발생 컴파일 할 때 :

Scanning dependencies of target hello 
[ 25%] Building C object CMakeFiles/hello.dir/libhello.c.obj 
[ 50%] Linking C static library libhello.a 
Error running link command: The system cannot find the file specified 
mingw32-make.exe[3]: *** [CMakeFiles\hello.dir\build.make:95: libhello.a] Error 2 
mingw32-make.exe[3]: *** Deleting file 'libhello.a' 
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/hello.dir/all] Error 2 
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:116: CMakeFiles/hellow.dir/rule] Error 2 
mingw32-make.exe: *** [Makefile:130: hellow] Error 2 

CMakeLists.txt 파일의 SET(CMAKE_C_ARCHIVE_FINISH true) 라인을 제거하고이 "정의 참조"오류 결과를 컴파일 :

[ 25%] Linking C static library libhello.a 
[ 50%] Built target hello 
Scanning dependencies of target hellow 
[ 75%] Building C object CMakeFiles/hellow.dir/hello.c.obj 
[100%] Linking C executable hellow.exe 
C:\...\Local\Temp\ccPctpZp.ltrans0.ltrans.o: In function `main': 
E:/Documents/MONAD/projects/ltotest/hello.c:4: undefined reference to `hello' 
collect2.exe: error: ld returned 1 exit status 
mingw32-make.exe[3]: *** [CMakeFiles\hellow.dir\build.make:97: hellow.exe] Error 1 
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:104: CMakeFiles/hellow.dir/all] Error 2 
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:116: CMakeFiles/hellow.dir/rule] Error 2 
mingw32-make.exe: *** [Makefile:130: hellow] Error 2 

nm libhello.a을 실행하면 libhello.c이 LTO로 컴파일됨을 보여줍니다.

libhello.c.obj: 
00000000 b .bss 
00000000 d .data 
00000000 r .gnu.lto_.decls.d7da3e90 
00000000 r .gnu.lto_.inline.d7da3e90 
00000000 r .gnu.lto_.opts 
00000000 r .gnu.lto_.refs.d7da3e90 
00000000 r .gnu.lto_.symbol_nodes.d7da3e90 
00000000 r .gnu.lto_.symtab.d7da3e90 
00000000 r .gnu.lto_hello.d7da3e90 
00000000 r .rdata$zzz 
00000000 t .text 
00000001 C ___gnu_lto_slim 
00000001 C ___gnu_lto_v1 

이것은 MinGW에서만 문제가되는 것 같습니다. 이전에 링크 된 답변에서 코드가 제대로 작동하는 것 같았습니다 (직접 테스트 할 수는 없었지만). 무엇이 잘못 될지에 대한 아이디어/어떻게 해결할 수 있습니까?

+0

@HansPassant gcc-ar.exe는 gcc LTO 플러그인을 제공하는 ar.exe의 래퍼입니다 (http://www.tin.org/bin/man.cgi?section=1&topic=gcc-ar-5 참조).) – cpdt

+0

@HansPassant 네, 찾을 수 있습니다 (게다가, 내가 알 수없는 exe를 언급하는 에러를 보게 될 것입니다.) – cpdt

답변

0

더 많은 연구가 있었고 ranlib에 대한 래퍼를 사용해야한다는 점을 언급 한 this page을 발견했습니다. 같이 SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>") 라인이 필요하지 않습니다처럼

cmake_minimum_required (VERSION 2.6) 
project (hellow) 
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto") 
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto") 
SET(CMAKE_AR "gcc-ar") 
SET(CMAKE_RANLIB "gcc-ranlib") 
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>") 
SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>") 
add_library(hello STATIC libhello.c) 
add_executable(hellow hello.c) 
target_link_libraries(hellow hello) 
add_dependencies(hellow hello) 

또한 보인다

을 (새로운 SET(CMAKE_RANLIB "gcc-ranlib") 라인을 확인할 수) :

는 는

물론 된만큼, 다음과 같은 내용에 CMakeLists.txt을 변경하면 프로젝트가 성공적으로 구축 할 수 있습니다 이 값은 CMAKE_C_ARCHIVE_FINISH의 기본값입니다.

+1

gcc docu는 또한'binutils의 차기 버전은 liblto_plugin의 자동 로딩을 지원할 것입니다 .'라고 언급했기 때문에 최근 binutils에서는 래퍼가 필요하지 않습니다. – ssbssa