1

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();
}
user3612009
  • 655
  • 1
  • 6
  • 18
  • Can you provide us a minimal example of your main function? – Simon Nov 16 '15 at 11:41
  • @Simon Edited the question to give main function. – user3612009 Nov 16 '15 at 14:15
  • 1
    I had this once with `#include ` in my sources (see also [here](http://sdl.libsdl.narkive.com/CKVjuSJU/why-do-i-need-a-winmain-if-i-include-windows-h-under-cygwin-but-not-if-i-use-msvc)). But there are a lot of possible causes (see e.g. [here](http://stackoverflow.com/questions/24586808) and more general [here](http://stackoverflow.com/questions/23918318)). If none of this helps, could you please add the command line that is called? Makefiles with verbose output are explained [here](http://stackoverflow.com/questions/2670121) – Florian Nov 16 '15 at 14:17
  • `WinMain` is required when linking a [Windows subsystem](https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx) executable. Somewhere in your build, the linker is being told to use the Windows subsystem. Run make with verbose debugging turned on to see the full command passed to the linker. – legalize Nov 21 '15 at 20:57

1 Answers1

3

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:

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • Yes I only have one main function. I am adding that file to the executable with other sources. One thing I did not mention here is that I am using CLion IDE here, but it only invokes cmake for building stuff. – user3612009 Nov 18 '15 at 10:58
  • @user3612009 Ok. Can you please check if my example code works from the command line in your Cygwin environment? Have you tried to add `#include ` to your main.cpp file? And could you please add the verbose makefile output to your question? Just to make sure there is e.g. no `-mwindows` in the linker call. – Florian Nov 18 '15 at 20:24