CMake to Makefile - Makefile gives `collect2: error: ld returned 1 exit status`

CMake to Makefile - Makefile gives `collect2: error: ld returned 1 exit status`

我正在尝试通过以下 this 示例来熟悉 Cmake: 简而言之,我有这个 c++ 代码。我想使用 Cmake 为这段代码生成一个 makefile,然后 运行 makefile。不幸的是,生成的 Makefile 给出了一个错误。我正在使用 Windows 7

这是我正在使用的代码

文件:main.cpp

#include "ToDo.h"
int main(  int    argc,  char** argv)
{
    ToDo list;

    return 0;
}

文件:ToDo.h

#ifndef TODO_H
#define TODO_H

class ToDo
{
public:
    ToDo();
    ~ToDo();
};

文件:ToDo.cpp

#include "ToDo.h"


ToDo::ToDo()
{
}

ToDo::~ToDo()
{
}
#endif // TODO_H

这是我的CMakeLists.txt

project("To Do List")


add_executable(toDo main.cpp ToDo.cpp)

这是我在 cygwin 中所做的

admin@US01WKS03044 /cygdrive/c/CmakeTest/build
$ cmake -G "Unix Makefiles" ..
-- The C compiler identification is unknown
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: C:/cygwin64/bin/cc
-- Check for working C compiler: C:/cygwin64/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working CXX compiler: C:/cygwin64/bin/c++.exe
-- Check for working CXX compiler: C:/cygwin64/bin/c++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Detecting CXX compile features
-- Detecting CXX compile features - failed
-- Configuring done
-- Generating done
-- Build files have been written to: C:/CmakeTest/build

admin@US01WKS03044 /cygdrive/c/CmakeTest/build
$ ls
cmake_install.cmake  CMakeCache.txt  CMakeFiles  Makefile

admin@US01WKS03044 /cygdrive/c/CmakeTest/build
$ make VERBOSE=1
"C:/Program Files (x86)/CMake/bin/cmake.exe" -HC:/CmakeTest -BC:/CmakeTest/build --check-build-system CMakeFiles/Makefile.cmake 0
"C:/Program Files (x86)/CMake/bin/cmake.exe" -E cmake_progress_start C:/CmakeTest/build/CMakeFiles C:/CmakeTest/build/CMakeFiles/progress.marks
/usr/bin/make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/cygdrive/c/CmakeTest/build'
/usr/bin/make -f CMakeFiles/toDo.dir/build.make CMakeFiles/toDo.dir/depend
make[2]: Entering directory `/cygdrive/c/CmakeTest/build'
"C:/Program Files (x86)/CMake/bin/cmake.exe" -E cmake_depends "Unix Makefiles" C:/CmakeTest C:/CmakeTest C:/CmakeTest/build C:/CmakeTest/build C:/CmakeTest/build/CMakeFiles/toDo.dir/DependInfo.cmake --color=
Dependee "C:/CmakeTest/build/CMakeFiles/toDo.dir/DependInfo.cmake" is newer than depender "C:/CmakeTest/build/CMakeFiles/toDo.dir/depend.internal".
Dependee "C:/CmakeTest/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "C:/CmakeTest/build/CMakeFiles/toDo.dir/depend.internal".
Scanning dependencies of target toDo
make[2]: Leaving directory `/cygdrive/c/CmakeTest/build'
/usr/bin/make -f CMakeFiles/toDo.dir/build.make CMakeFiles/toDo.dir/build
make[2]: Entering directory `/cygdrive/c/CmakeTest/build'
"C:/Program Files (x86)/CMake/bin/cmake.exe" -E cmake_progress_report C:/CmakeTest/build/CMakeFiles 1
[ 50%] Building CXX object CMakeFiles/toDo.dir/main.cpp.o
C:/cygwin64/bin/c++.exe     -o CMakeFiles/toDo.dir/main.cpp.o -c C:/CmakeTest/main.cpp
"C:/Program Files (x86)/CMake/bin/cmake.exe" -E cmake_progress_report C:/CmakeTest/build/CMakeFiles 2
[100%] Building CXX object CMakeFiles/toDo.dir/ToDo.cpp.o
C:/cygwin64/bin/c++.exe     -o CMakeFiles/toDo.dir/ToDo.cpp.o -c C:/CmakeTest/ToDo.cpp
Linking CXX executable toDo.exe
"C:/Program Files (x86)/CMake/bin/cmake.exe" -E remove -f CMakeFiles/toDo.dir/objects.a
C:/cygwin64/bin/ar.exe cr CMakeFiles/toDo.dir/objects.a @CMakeFiles/toDo.dir/objects1.rsp
C:/cygwin64/bin/c++.exe     -Wl,--whole-archive CMakeFiles/toDo.dir/objects.a -Wl,--no-whole-archive  -o toDo.exe -Wl,--out-implib,libtoDo.dll.a -Wl,--major-image-version,0,--minor-image-version,0
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-1.7.33-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-1.7.33-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status
make[2]: *** [toDo.exe] Error 1
make[2]: Leaving directory `/cygdrive/c/CmakeTest/build'
make[1]: *** [CMakeFiles/toDo.dir/all] Error 2
make[1]: Leaving directory `/cygdrive/c/CmakeTest/build'
make: *** [all] Error 2

为什么 Makefile 失败了?

该错误意味着编译器正在寻找 WinMain 作为可执行文件的入口点,而不是经典的 main,但找不到它。

因为我不使用 cygwin,所以不能真正说出原因(WinMain 特定于 windows gui 应用程序,如果我没记错的话),但你可以重命名 mainWinMain 看看它是否有效。或参见 http://log4aj.blogspot.ro/2010/11/cygwin-gcc-undefined-reference-to.html.