我试图在 Windows 7 中使用 cygwin 构建可执行文件,但在链接阶段出现以下错误。
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-2.3.0-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-2.3.0-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status
我不明白为什么它会尝试寻找 WinMain。我有一个 cpp 文件,其中定义了 int main(int, char**) 函数,它是构建的一部分。我不明白为什么它试图使用 libcmain.c,它是可执行文件创建期间 cygwin 的一部分。我正在使用 cmake,它具有以下内容:
add_executable(binary_name ${SOURCE_FILES})
此应用程序需要是控制台类型的应用程序并且没有 GUI。
编辑: 我的主要功能如下。
#include "gtest/gtest.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
最佳答案
我建议检查三点:
- 检查通过激活详细输出调用的命令行。参见 Using CMake with GNU Make: How can I see the exact commands?
- 你确定你真的只有一个
main()
吗?在您的代码中发挥作用? - 源文件是否包含
main()
函数直接添加到add_executable()
打电话?
因为我刚刚使用 Cygwin 中可用的最新 CMake 包尝试了您的代码,它似乎工作得很好(在 #include <windows.h>
中使用和不使用 main.cpp
)。
希望它能帮助您找到问题,这是我所做的:
$ export PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin
$ cmake --version
cmake version 3.3.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ c++ -v --version
c++ (GCC) 4.9.3
gcc-Version 4.9.3 (GCC)
GNU C (GCC) Version 4.9.3 (x86_64-pc-cygwin)
GNU assembler version 2.25 (x86_64-pc-cygwin) using BFD version (GNU Binutils) 2.25
GNU assembler (GNU Binutils) 2.25
GNU ld (GNU Binutils) 2.25
$ cmake ..
-- The CXX compiler identification is GNU 4.9.3
-- Check for working CXX compiler: /usr/bin/c++.exe
-- Check for working CXX compiler: /usr/bin/c++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found GTest: /cygdrive/c/gtest-1.7.0/lib/.libs/libgtest.a
-- Configuring done
-- Generating done
-- Build files have been written to: ...
Scanning dependencies of target binary_name
[ 50%] Building CXX object CMakeFiles/binary_name.dir/main.cpp.o
[100%] Linking CXX executable binary_name.exe
[100%] Built target binary_name
$ ./binary_name.exe
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (3 ms total)
[ PASSED ] 0 tests.
我使用了以下 CMakeLists.txt
:
cmake_minimum_required(VERSION 3.0)
project(binary_name CXX)
enable_testing()
set(GTEST_ROOT /cygdrive/c/gtest-1.7.0)
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${GTEST_ROOT}/lib/.libs")
find_package(GTest REQUIRED)
add_executable(binary_name main.cpp)
target_include_directories(binary_name PRIVATE ${GTEST_INCLUDE_DIRS})
target_link_libraries(binary_name PRIVATE ${GTEST_LIBRARIES})
导致以下链接行 CMakeFiles/binary_name.dir/link.txt
:
/usr/bin/c++.exe
-Wl,--enable-auto-import CMakeFiles/binary_name.dir/main.cpp.o
-o binary_name.exe
-Wl,--out-implib,libbinary_name.dll.a
-Wl,--major-image-version,0,--minor-image-version,0
/cygdrive/c/gtest-1.7.0/lib/.libs/libgtest.a
更多引用资料
与SDL 相结合的类似问题的大多数引用或更一般:
关于c++ - 使用 cygwin 和 cmake 对 `WinMain' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33734016/