'wxWidgets from mingw-w64 can't be used in CLion

i can build wxwidgets 3.1.6 by mingw32 and used in CLion or CodeBlock on Windows.

but when i use mingw-w64 to compiled wxwidgets lib on new computer, it will work well in codeblock, but it will not be able to use lib in CLion. I always receive the following message:

CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `WinMain':
C:/Project/untitled/main.cpp:31: undefined reference to `wxEntry(HINSTANCE__*, HINSTANCE__*, char*, int)'
CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `wxCreateApp()':
C:/Project/untitled/main.cpp:31: undefined reference to `wxAppConsoleBase::CheckBuildOptions(char const*, char const*)'
CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `MyFrame::MyFrame(wxString const&, wxPoint const&, wxSize const&)':
C:/Project/untitled/main.cpp:39: undefined reference to `wxString::FromAscii(char const*)'
C:/Project/untitled/main.cpp:48: undefined reference to `wxMenuBar::wxMenuBar()'
C:/Project/untitled/main.cpp:51: undefined reference to `wxFrameBase::SetMenuBar(wxMenuBar*)'
C:/Project/untitled/main.cpp:52: undefined reference to `wxString::FromAscii(char const*)'
C:/Project/untitled/main.cpp:52: undefined reference to `wxFrameBase::CreateStatusBar(int, long, int, wxString const&)'
C:/Project/untitled/main.cpp:53: undefined reference to `wxFrameBase::SetStatusText(wxString const&, int)'
C:/Project/untitled/main.cpp:39: undefined reference to `wxFrame::~wxFrame()'
CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `MyFrame::OnExit(wxCommandEvent&)':
C:/Project/untitled/main.cpp:57: undefined reference to `wxWindowBase::Close(bool)'
CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `MyFrame::OnAbout(wxCommandEvent&)':
C:/Project/untitled/main.cpp:61: undefined reference to `wxMessageBox(wxString const&, wxString const&, long, wxWindow*, int, int)'
CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `MyFrame::OnHello(wxCommandEvent&)':
C:/Project/untitled/main.cpp:66: undefined reference to `wxString::FromAscii(char const*)'
CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `__tcf_1':
C:/Project/untitled/main.cpp:26: undefined reference to `wxEventHashTable::~wxEventHashTable()'
CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `__static_initialization_and_destruction_0':

and more

this msg look like unsuccessful use of lib, but there is no mention of "cannot find lib" in clion. I read many articles, they all show that this is related to c++11, but I use C++14.

The following is the shell I built wxwidgets in wxWidgets_3.1.6/build/msw:

mingw32-make -f makefile.gcc clean
mingw32-make -f makefile.gcc BUILD=debug SHARE=0 MONOLITHIC=1 UNICODE=1
mingw32-make -f makefile.gcc BUILD=release SHARE=0 MONOLITHIC=1 UNICODE=1

cmakelists.txt


macro(includeAllSubDirectory subDir)
 message("include ./${CMAKE_CURRENT_SOURCE_DIR}/${subDir}")
 include_directories(${CMAKE_CURRENT_LIST_DIR}/${subDir})
 FILE(GLOB children RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/${subDir} ${CMAKE_CURRENT_SOURCE_DIR}/${subDir}/*)
 foreach (child ${children})
     if (IS_DIRECTORY ${CMAKE_SOURCE_DIR}/${subDir}/${child})
         includeAllSubDirectory(${subDir}/${child})
     endif ()
 endforeach ()
endmacro()

#===============================
# Init Project
#===============================
project(untitled)
cmake_minimum_required(VERSION 3.19)
set(CMAKE_CXX_STANDARD 14)
aux_source_directory(. SRC_LIST)
include_directories(.)
add_executable(${PROJECT_NAME} ${SRC_LIST})

#printAllVariables()
#set(CMAKE_VERBOSE_MAKEFILE TRUE)

#===============================
# Init wxWidgets Lib
#===============================
set(wxWidgets_CONFIGURATION mswud)
find_package(wxWidgets COMPONENTS REQUIRED)
include(${wxWidgets_USE_FILE})
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})

#===============================
# include
#===============================
includeAllSubDirectory(somefolder)

what did I miss?



Solution 1:[1]

Perhaps you don't set some required variables in the CMakeLists.txt.

I don't need own compiled wxWidgets libs, but all it works "out of box" with the downloaded binaries from official website https://www.wxwidgets.org/downloads#v3.1.5_msw -> "MinGW-w64 8.1", "Header Files" ("include" folder), "64-Bit (x86_64)" "Development Files" ("lib" folder).

My CMakeLists.txt

cmake_minimum_required(VERSION 3.21)
project(temp)

set(CMAKE_CXX_STANDARD 14)

aux_source_directory(. SRC_LIST)

set(wxWidgets_ROOT_DIR "../../../wxWidgets-3.1.5/")
set(wxWidgets_LIB_DIR "${wxWidgets_ROOT_DIR}lib/gcc810_x64_dll")
set(wxWidgets_LIBRARIES "${wxWidgets_ROOT_DIR}lib")
set(wxWidgets_INCLUDE_DIRS "${wxWidgets_ROOT_DIR}include")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(wxWidgets_CONFIGURATION mswud)
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(wxWidgets_CONFIGURATION mswu)
endif()
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})

add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1