未定义的引用`WinMain'与cygwin和cmake

时间:2015-11-16 11:13:51

标签: c++ cmake cygwin

我正在尝试使用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();
}

1 个答案:

答案 0 :(得分:2)

我建议检查三点:

  1. 通过激活详细输出检查调用的命令行。请参阅Using CMake with GNU Make: How can I see the exact commands?
  2. 您确定代码中只有一个main()函数吗?
  3. 包含main()功能的源文件是否直接添加到add_executable()来电?
  4. 因为我刚刚使用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

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>My Dashboard</title>
    </head>
    
    <body>
    
    
    
    <div id="dashboard" class="row" >
    
        <div  class="col-4" style="background-color:#009">
        </div>
    
        <div class="col-4" style="background-color:#C00">
        </div>
    
        <div class="col-4" style="background-color:#0C0">
        </div>
    
    </div> 
    
    
    
    <div id="alarm" class="row" >
    
        <div class="col-12" style="background-color:#009">
        </div>
    
    </div>
    
    <div id="calendar" class="row" >
    
        <div class="col-12" style="background-color:C00">
        </div>
    
    </div>
    
    <div id="weather" class="row" >
    
        <div class="col-12" style="background-color:#0C0">
        </div>
    
    </div>
    
    
    </body>
    </html>
    

    更多参考资料

    大多数提到类似问题,与SDL结合使用或更为通用: