user3612009
user3612009

Reputation: 655

undefined reference to `WinMain' with cygwin and cmake

I am trying to build a executable with cygwin in windows 7 and I get the following error in the linking stage.

/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

I dont understand why it tries to find WinMain. I have a cpp file with int main(int, char**) function defined which is part of the build. I do not understand why it is trying to use libcmain.c, which is part of cygwin during executable creation. I am using cmake and it has following:

add_executable(binary_name ${SOURCE_FILES})

This application need to be a console type application and does not have a GUI.

EDIT: My main function is as follows.

#include "gtest/gtest.h"
int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Upvotes: 1

Views: 12378

Answers (1)

Florian
Florian

Reputation: 42872

Three points I would recommend to check:

  1. Check the command line(s) that are called by activating verbose output. See Using CMake with GNU Make: How can I see the exact commands?
  2. Are you sure that you really have only one main() function in your code?
  3. Is the source file containing main() function directly added to the add_executable() call?

Because I just have given your code a try with the latest CMake package available in Cygwin and it seems to work just fine (with and without #include <windows.h> in main.cpp).

In the hope that it might help finding your problem, here is what I've done:

$ 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.

I've used the following 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})

Resulting in the following link line 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 

More References

Most references to similar problems where in combination with SDL or far more general:

Upvotes: 3

Related Questions