저는 크로스 플랫폼 C ++ 프로젝트를 개발하고 있습니다. Linux에서는 잘 구축되지만 Windows (10) + MSys2에서는 이상한 문제가 발생했습니다. 컴파일은 잘 작동하지만 (내 include dirs 등을 선택합니다.) 내가 가지고있는 정적 가져온 라이브러리와 C ++ 런타임에 대한 모든 종류의 정의되지 않은 참조 오류로 인해 연결이 실패합니다.
CMAKE_C [XX] _COMPILER, CMAKE_MAKE_PROGRAM 설정을 시도했지만 구성 단계의 출력은 항상 동일합니다.
$ cmake ..
-- The C compiler identification is GNU 10.2.0
-- The CXX compiler identification is GNU 10.2.0
System is unknown to cmake, create:
Platform/MINGW64_NT-10.0-19041 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
-- Detecting C compiler ABI info
System is unknown to cmake, create:
Platform/MINGW64_NT-10.0-19041 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
System is unknown to cmake, create:
Platform/MINGW64_NT-10.0-19041 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /mingw64/bin/CC.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: <....>
앞서 언급했듯이 컴파일은 작동하지만 실행 파일 연결은 눈에 띄게 실패합니다. 다음은 최소한의 작업 예입니다.
$ cat ../CMakeLists.txt
project(example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(example
main.cpp
)
다음은 출력 샘플입니다 (나머지는 간결함을 위해 생략 됨).
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.cpp.obj:main.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.cpp.obj:main.cpp:(.text+0x81): undefined reference to `std::ios_base::Init::Init()'
cmake에 -v를 추가하면 다음 명령이 생성됩니다.
엮다:
/mingw64/bin/CC.exe -std=gnu++17 -o CMakeFiles/example.dir/main.cpp.obj -c /home/.../Development/minex/main.cpp
링크:
/mingw64/bin/CC.exe CMakeFiles/example.dir/main.cpp.obj -o example
CC.exe가 꺼진 것 같고 CXX 컴파일러 플래그를 설정하거나 설정하지 않으면 사용됩니다.
또한 "MSYS2 Makefiles"생성을 시도했지만 실패했습니다 (생성기를 알지 못함).
실행하는 것으로 출력을 재현 할 수 있습니다
$ CC main.cpp -o example
동안
$ g++ main.cpp -o example
잘 작동합니다.
CMake 버전은 3.18.4입니다.
편집 : 이것은 실행의 전체 출력입니다 make VERBOSE=1
( mingw64-cmake
'입력 디렉토리'와 '이탈 디렉토리'경로가 절대 Windows 경로 임을 제외하고는 동일한 출력을 생성하는 것 같습니다) :
$ cat log
/usr/bin/cmake.exe -S/home/<...>/Development/minex -B/home/<...>/Development/minex/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake.exe -E cmake_progress_start /home/<...>/Development/minex/build/CMakeFiles /home/<...>/Development/minex/build//CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/<...>/Development/minex/build'
make -f CMakeFiles/example.dir/build.make CMakeFiles/example.dir/depend
make[2]: Entering directory '/home/<...>/Development/minex/build'
cd /home/<...>/Development/minex/build && /usr/bin/cmake.exe -E cmake_depends "Unix Makefiles" /home/<...>/Development/minex /home/<...>/Development/minex /home/<...>/Development/minex/build /home/<...>/Development/minex/build /home/<...>/Development/minex/build/CMakeFiles/example.dir/DependInfo.cmake --color=
Dependee "/home/<...>/Development/minex/build/CMakeFiles/example.dir/DependInfo.cmake" is newer than depender "/home/<...>/Development/minex/build/CMakeFiles/example.dir/depend.internal".
Dependee "/home/<...>/Development/minex/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/<...>/Development/minex/build/CMakeFiles/example.dir/depend.internal".
Scanning dependencies of target example
make[2]: Leaving directory '/home/<...>/Development/minex/build'
make -f CMakeFiles/example.dir/build.make CMakeFiles/example.dir/build
make[2]: Entering directory '/home/<...>/Development/minex/build'
[ 50%] Building CXX object CMakeFiles/example.dir/main.obj
/mingw64/bin/CC.exe -std=gnu++17 -o CMakeFiles/example.dir/main.obj -c /home/<...>/Development/minex/main.cpp
[100%] Linking CXX executable example
/usr/bin/cmake.exe -E cmake_link_script CMakeFiles/example.dir/link.txt --verbose=1
/mingw64/bin/CC.exe CMakeFiles/example.dir/main.obj -o example
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.text+0x23): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.text+0x32): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.text+0x81): undefined reference to `std::ios_base::Init::Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_[.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_]+0x0): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/example.dir/build.make:103: example] Error 1
make[2]: Leaving directory '/home/<...>/Development/minex/build'
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/example.dir/all] Error 2
make[1]: Leaving directory '/home/<...>/Development/minex/build'
make: *** [Makefile:103: all] Error 2
해결책:
CMAKE_CXX_COMPILER를 잘못 설정했습니다 : /. 나는 기억에서 그것을하고 있었고
CMAKE_CXX_COMPILER=... cmake ..
아니
cmake .. -DCMAKE_CXX_COMPILER=...
하나! CC가 cpp 파일을 성공적으로 컴파일하는 데 사용되는 것이 여전히 이상하지만 개체 파일을 연결할 수 없습니다.
ac 컴파일러로 C ++ 프로그램을 컴파일 / 링크하려고하기 때문에 이러한 오류가 발생합니다. 예를 들어 언급 한 두 개의 정의되지 않은 참조는 libstdc ++의 일부입니다. 를 사용할 때 기본적으로 사용 g++
되지만 CC
. CC를 사용하려면 수동으로 추가해야 -lstdc++
합니다.
가장 쉬운 방법은 g ++를 사용하여 C ++ 프로그램을 컴파일하고 링크하는 것입니다.
어떤 이유로 /mingw64/bin/CC.exe는 CXX 컴파일러로 간주되며 작동 감지를 건너 뜁니다. 작업 감지 건너 뛰기를 방지하려면 추가 할 수 있습니다 set(CMAKE_CXX_COMPILER_WORKS 1)
. 컴파일러를 수정하려면 CMAKE_CXX_COMPILER에CMAKE_CXX_COMPILER
설명 된 대로 설정하거나 CXX에CXX
설명 된대로 설정할 수 있습니다 . 캐시를 청소할 때주의하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다