2

I have the following problem. I want to create a cross-platform GUI application that uses wxWidgets I am trying to compile and run a "Hello World" example from wxWidgets web site. I want to use cmake to build the project. On Linux everything works without problems, but on Windows I see the following linking error

MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

It looks like the linker is looking for main function for some reason (shouldn't it look for WinMain?) The example code has wxIMPLEMENT_APP(MyApp); macro, I have also set the linker option set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /subsystem:windows"), but nothing helps, so I don't understand what is the problem. I have a feeling that I missed something simple. May be you had a similar problem before with one of your projects? Thank you for your help.

My CMakeLists.txt file is given below

cmake_minimum_required (VERSION 3.8)

project ("project")

set(CMAKE_CXX_STANDARD 17)
set(WXWIN "C:/msys64/wxWidgets")


# =========================================================================
set(BUILD_DEPS ON)
list(APPEND CMAKE_MODULE_PATH "include/libs")

include_directories("include/libs")
add_subdirectory("include/libs/glog")

if (MSVC)
set(wxWidgets_ROOT_DIR ${WXWIN})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /subsystem:windows")
endif (MSVC)

find_package(wxWidgets COMPONENTS net gl core base)
include(${wxWidgets_USE_FILE})
set(wxWidgets_USE_UNICODE ON)

# Create shared library for the project
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(PROJECT_LINK_LIBS Tools)
set(LIBRARY_SRC include/Tools.cpp)
add_library(Tools SHARED ${LIBRARY_SRC})
link_directories("out/build")
set_target_properties(Tools PROPERTIES POSITION_INDEPENDENT_CODE 1)
# =========================================================================

# Add source to this project's executable.
add_executable (project "project.cpp" "project.h")
target_compile_features(project PUBLIC cxx_std_17)
target_link_libraries (project ${PROJECT_LINK_LIBS} ${wxWidgets_LIBRARIES})
I Rom
  • 161
  • 6
  • Does this answer your question? [Error LNK2019 unresolved external symbol \_main referenced in function "int \_\_cdecl invoke\_main(void)" (?invoke\_main@@YAHXZ)](https://stackoverflow.com/questions/33400777/error-lnk2019-unresolved-external-symbol-main-referenced-in-function-int-cde) – Kevin May 26 '20 at 12:03

1 Answers1

3

It turns out that to make it work in need to put WIN32 into add_executable command of cmake add_executable (project "project.cpp" "project.h") => add_executable (project WIN32 "project.cpp" "project.h")

I Rom
  • 161
  • 6