I am trying to make a CMAKE file that allows me to cross compile a WxWidgets application From Ubuntu For Windows.
My tools are all installed correctly, and I am able to run:
i686-w64-mingw32.static-gcc main.cpp $(i686-w64-mingw32.static-wx-config --cxxflags --libs) -o main.exe -std=c++11 -lstdc++
and produce a working output.
But I have been unsuccessful in finding a way to convert this into a CMAKE file. Does anyone have any resources or even examples of a working example?
Here is an example of my non-working file:
cmake_minimum_required(VERSION 3.11)
SET (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
SET(PROJECT_NAME "WxWidgets")
SET(CMAKE_C_COMPILER i686-w64-mingw32.static)
project(
${PROJECT_NAME}
VERSION 1.0
LANGUAGES CXX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} (i686-w64-mingw32.static-wx-config --cxxflags --libs) -L /home/kevin/wxWidgets/include/ -Wall -std=gnu++2a -static -g")
file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/src/*.h"
"${PROJECT_SOURCE_DIR}/src/*.hpp"
"${PROJECT_SOURCE_DIR}/src/*.c"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.c"
"${PROJECT_SOURCE_DIR}/src/**/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.hpp"
)
add_executable(${PROJECT_NAME} ${all_SRCS})
I am running the command:
i686-w64-mingw32.static-cmake ..
within my build directory.
By removing:
**(i686-w64-mingw32.static-wx-config --cxxflags --libs) -L /home/kevin/wxWidgets/include/**
...I am getting the error:
fatal error: wx/wxprec.h: No such file or directory
8 | #include "wx/wxprec.h"
I have this file I checked with locate:
$ locate wx/wxprec.h
/home/kevin/mxe/usr/i686-w64-mingw32.static/include/wx-3.1/wx/wxprec.h
/home/kevin/wxWidgets/include/wx/wxprec.h
/usr/local/include/wx-3.3/wx/wxprec.h
EDIT:
Thanks to #vre's comment, I have updated my CMakeLists.txt file and have moved closer, it seems now that I just need to link correctly Here is what I have:
cmake_minimum_required(VERSION 3.11)
SET (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
SET(PROJECT_NAME "WxWidgets")
SET(CMAKE_C_COMPILER i686-w64-mingw32.static)
project(
${PROJECT_NAME}
VERSION 1.0)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++2a -static -g")
file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/src/*.h"
"${PROJECT_SOURCE_DIR}/src/*.hpp"
"${PROJECT_SOURCE_DIR}/src/*.c"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.c"
"${PROJECT_SOURCE_DIR}/src/**/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.hpp"
)
add_executable(${PROJECT_NAME} ${all_SRCS})
include( "${wxWidgets_USE_FILE}" )
find_package(wxWidgets REQUIRED)
target_include_directories(${PROJECT_NAME} PUBLIC /home/kevin/mxe/usr/i686-w64-mingw32.static/include/wx-3.1/ /home/kevin/mxe/usr/i686-w64-mingw32.static/lib/wx/include/i686-w64-mingw32.static-msw-unicode-static-3.1/)
target_link_directories(${PROJECT_NAME} PRIVATE /home/kevin/mxe/usr/i686-w64-mingw32.static/lib/)
But sadly, I have many linker errors such as:
undefined reference to `wxFrame::GetDefaultIcon() const'
Related
I have a project where I link to boost; the CMakeLists.txt looks like this:
project(test VERSION 1.0)
set(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op -Wredundant-decls -Wshadow -Woverloaded-virtual")
find_package(Boost COMPONENTS filesystem program_options log log_setup REQUIRED)
include_directories(
${Boost_INCLUDE_DIRS}
)
add_executable(test src/test.cpp)
target_link_libraries(test
LINK_PUBLIC
${Boost_LIBRARIES}
config++
)
install(TARGETS test DESTINATION /usr/bin)
Now I am stumped how the linking in CMake works under the hood. It compiles fine but after I upgraded Boost, I am getting the error
test: error while loading shared libraries: libboost_program_options.so.1.75.0: cannot open shared object file: No such file or directory
and I'd like to avoid having to recompile every time a library gets an update.
The CMake doc says that
The LINK_PUBLIC and LINK_PRIVATE modes can be used to specify both the link dependencies and the link interface in one command.
But I don't quite understand how or why CMake links to the specific version of Boost and not simply to /usr/lib/libboost_program_options.so which is just a symlink to the currently installed version.
I am compiling a google test project and the compilation is successful. However I am getting the following error during linking with my project.
Is this related to how the linking is ordered I wonder?
CMakeFiles/Test.dir/Test1.cpp.o: In function `GetTimeMock::GetTime(int*, _time_data*)':
Test1.cpp:(.text._ZN20GetTimeMock19ENET_GetTimeEPiP19_time_data[_ZN20EnetGetTimeMock19ENET_GetTimeEPiP19_time_data]+0x4a): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
Here is what my CMAKE looks like
make_minimum_required(VERSION 3.2.2)
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
# Set compiler to c++17
#set(CMAKE_CXX_STANDARD 17)
ExternalProject_Add(
GoogleTest
URL https://github.com/google/googletest/archive/6ce9b98f541b8bcd84c5c5b3483f29a933c4aefb.zip
URL_MD5 "0bca4c8c9fc2d06634185057a905ff11"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/external"
BUILD_COMMAND "$(MAKE)"
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -O2 -DNDEBUG -Wall -Wextra -pedantic")
set(SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../source)
set(UNIT_TEST_MAIN ${CMAKE_CURRENT_SOURCE_DIR}/tests/unit_test_main.cpp)
set(GTEST_MAIN ${CMAKE_BINARY_DIR}/external/lib/libgtest_main.a)
set(GTEST ${CMAKE_BINARY_DIR}/external/lib/libgtest.a)
set(GMOCK_MAIN ${CMAKE_BINARY_DIR}/external/lib/libgmock_main.a)
set(GMOCK ${CMAKE_BINARY_DIR}/external/lib/libgmock.a)
set(SOURCE
${UNIT_TEST_MAIN}
${SOURCE_PATH}test.cpp
${SOURCE_PATH}/test1.cpp
${SOURCE_PATH}/test2.cpp
${CMAKE_CURRENT_SOURCE_DIR}/bar.cpp
${CMAKE_CURRENT_SOURCE_DIR}/bar1.cpp
${STUB_PATH}/platforms/k64f/src/fstub.cpp
)
include_directories(
${SOURCE_PATH}/include
${SOURCE_PATH}/Files
${SOURCE_PATH}/
)
set(target "Test")
add_definitions(${DEFINITION})
add_executable("${target}" ${SOURCE})
add_dependencies("${target}" GoogleTest)
target_link_libraries("${target}"
"${GMOCK_MAIN}"
"${GMOCK}"
"${GTEST_MAIN}"
"${GTEST}"
pthread)
I have C++ code which uses FFTW 3.3.4. Ubuntu 16.04, cmake version 3.7.2
$ locate *fftw*.so
/usr/lib/libsfftw.so
/usr/lib/libsfftw_mpi.so
/usr/lib/libsfftw_threads.so
/usr/lib/libsrfftw.so
/usr/lib/libsrfftw_mpi.so
/usr/lib/libsrfftw_threads.so
/usr/lib/x86_64-linux-gnu/libfftw3.so
/usr/lib/x86_64-linux-gnu/libfftw3_mpi.so
/usr/lib/x86_64-linux-gnu/libfftw3_omp.so
/usr/lib/x86_64-linux-gnu/libfftw3_threads.so
/usr/lib/x86_64-linux-gnu/libfftw3f.so
/usr/lib/x86_64-linux-gnu/libfftw3f_mpi.so
/usr/lib/x86_64-linux-gnu/libfftw3f_omp.so
/usr/lib/x86_64-linux-gnu/libfftw3f_threads.so
/usr/lib/x86_64-linux-gnu/libfftw3l.so
/usr/lib/x86_64-linux-gnu/libfftw3l_mpi.so
/usr/lib/x86_64-linux-gnu/libfftw3l_omp.so
/usr/lib/x86_64-linux-gnu/libfftw3l_threads.so
/usr/lib/x86_64-linux-gnu/libfftw3q.so
/usr/lib/x86_64-linux-gnu/libfftw3q_omp.so
/usr/lib/x86_64-linux-gnu/libfftw3q_threads.so
$ locate fftw3.h
/usr/include/fftw3.h
I can compile it in this way:
g++ main.cpp -o main -lfftw3
but I have a problem with cmake.
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.5.1)
project (main)
SET(CMAKE_C_COMPILER gcc)
SET(CMAKE_CXX_COMPILER g++)
file(GLOB SOURCES "*.cpp")
SET(CMAKE_CXX_FLAGS "-lm -lfftw3")
SET(CMAKE_C_FLAGS "-lm -lfftw3")
INCLUDE_DIRECTORIES(/usr/include)
LINK_DIRECTORIES(/usr/lib/x86_64-linux-gnu)
add_library(fftw3 STATIC IMPORTED)
set(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)
add_executable(main ${SOURCES})
cmake . && make
gives
undefined reference to `fftw_malloc'
and the same for the other fftw functions.
The command add_library will create a library in your project (CMake -
add_library). I assume that is not what you want.
The command: g++ main.cpp -o main -lfftw3 will link the executable to the fftw library. In CMake you can reproduce the linking with:
add_executable(main ${SOURCES})
target_link_libraries(main fftw3)
Docu: CMake - target_link_libraries
Notice: It is important that the add_executable command comes before the linking.
Have fun with FFTW :)
We delegate this to pkg-config:
find_package(PkgConfig REQUIRED)
pkg_search_module(FFTW REQUIRED fftw3 IMPORTED_TARGET)
include_directories(PkgConfig::FFTW)
link_libraries (PkgConfig::FFTW)
This works with cmake 3.11 (at least, it may work with earlier versions too).
NOTE: This doesn't work with fftw3_thread component because they don't have a separate .pc file. (see https://github.com/FFTW/fftw3/issues/180).
This may work to add the component (not tested, doesn't work in Macs --see comments--):
link_libraries (PkgConfig::FFTW -lfftw3_thread)
NOTE 2: I am pasting here #OlafWilkocx solution to get the thread component as well
cmake_minimum_required(VERSION 3.20)
...
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fno-math-errno -ffinite-math-only") # clang
find_package(OpenMP REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(FFTW IMPORTED_TARGET REQUIRED fftw3)
if( NOT FFTW_ROOT AND DEFINED ENV{FFTWDIR} )
set( FFTW_ROOT $ENV{FFTWDIR} )
endif()
find_library(
FFTW_DOUBLE_THREADS_LIB
NAMES "fftw3_threads"
PATHS ${PKG_FFTW_LIBRARY_DIRS} ${LIB_INSTALL_DIR}
)
if (FFTW_DOUBLE_THREADS_LIB)
set(FFTW_DOUBLE_THREADS_LIB_FOUND TRUE)
set(FFTW_LIBRARIES ${FFTW_LIBRARIES} ${FFTW_DOUBLE_THREADS_LIB})
add_library(FFTW::DoubleThreads INTERFACE IMPORTED)
set_target_properties(FFTW::DoubleThreads
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFTW_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${FFTW_DOUBLE_THREADS_LIB}"
)
else()
set(FFTW_DOUBLE_THREADS_LIB_FOUND FALSE)
endif()
include_directories(PkgConfig::FFTW)
add_executable(solver_step src/solver_step.cc)
target_link_libraries(solver_step PRIVATE OpenMP::OpenMP_CXX ${VTK_LIBRARIES} PkgConfig::FFTW ${FFTW_DOUBLE_THREADS_LIB})
NOTE 3
I am told that the line include_directories(PkgConfig::FFTW) is always incorrect and suggested to either only use link_libraries(PkgConfig::FFTW) or target_link_libraries(target_name PRIVATE PkgConfig::FFTW).
see here: Avoid bad include paths in CMake's pkg-config fallback
I have build/CMakeLists.txt file:
cmake_minimum_required (VERSION 2.8)
project(Tetris)
include_directories(
"headers"
)
set(SFML_DEPENDENCIES)
set(SFML_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} ${SFML_DEPENDENCIES})
set(SFML_DEPENDENCIES ${SFML_GRAPHICS_DEPENDENCIES} ${SFML_DEPENDENCIES})
set(SFML_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} ${SFML_DEPENDENCIES})
set(CMAKE_CXX_COMPILER /usr/bin/g++-5)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")
set(EXECUTABLE_NAME "tetris")
file(GLOB SOURCES *.cpp)
add_executable(${EXECUTABLE_NAME} ${SOURCES})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
When I run cmake .. && make I have a screen of messages saying I did not include SFML library:
undefined reference to `sf::VertexArray::operator[](unsigned long)'
undefined reference to `sf::Keyboard::isKeyPressed(sf::Keyboard::Key)'
and so on. I also tried to include flags to CMAKE_CXX_FLAGS variable:
-lsfml-graphics -lsfml-window -lsfml-system it doesn't work either. But if I run g++ from the console directly all compiles just fine:
g++-5 --std=c++11 -Wall main.cpp Game.cpp Window.cpp Board.cpp -lsfml-graphics -lsfml-window -lsfml-system
I use Linux Mint if it has any difference.
You need to find the location of SFML package, and add include directories and link.
In your CMakeLists.txt:
Change this line:
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
By:
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
Source
EDITED:
For SFML v1, change find_package line by:
find_package(SFML 1 REQUIRED system window graphics)
I am using these steps (line 42 in the 2nd sourcecode place). However, I reading/writing to files with .h5 extension, where the code needs surely this flag: -lhdf5.
In order to compile the functions for hdf5, I would do something like this:
g++ -std=c++0x main.cpp -lhdf5
Notice that the flag must be placed at the end of the compilation command, as stated in this answer.
I updated my question, due to a comment.
So, I modified the CMakeLists.txt and what I changed was this part:
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++0x")
set(CMAKE_EXE_LINKER_FLAGS "-lhdf5 -lhdf5_hl -lhdf5_cpp")
However, when I execute make, it seems that hdf5 is not found.
EDIT
With Marc's suggestion, I got:
Linking CXX executable exe
/usr/bin/cmake -E cmake_link_script CMakeFiles/exe.dir/link.txt --verbose=1
/usr/bin/c++ -frounding-math -O3 -DNDEBUG -lhdf5 -lhdf5_hl -lhdf5_cpp CMakeFiles/exe.dir/match.cpp.o -o exe -rdynamic -L/home/samaras/code/CGAL-4.3/lib -L/usr/local/lib -lmpfr -lgmp /home/samaras/code/CGAL-4.3/lib/libCGAL.so -lboost_thread-mt -lpthread /usr/local/lib/libboost_system.so /home/samaras/code/CGAL-4.3/lib/libCGAL.so -lboost_thread-mt -lpthread /usr/local/lib/libboost_system.so -Wl,-rpath,/home/samaras/code/CGAL-4.3/lib:/usr/local/lib
and here is the problem, I think (see the answer I linked too). The linker flag of hdf5 is NOT at the end.
How to put it at the end? Maybe I am using the wrong set()?
EDIT - solution
Here is the working CMakeLists.txt:
# Created by the script cgal_create_cmake_script_with_options
# This is the CMake script for compiling a set of CGAL applications.
project( exe )
cmake_minimum_required(VERSION 2.6.2)
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 2.6)
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}" VERSION_GREATER 2.8.3)
cmake_policy(VERSION 2.8.4)
else()
cmake_policy(VERSION 2.6)
endif()
endif()
set( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true )
if ( COMMAND cmake_policy )
cmake_policy( SET CMP0003 NEW )
endif()
# CGAL and its components
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++0x")
find_package( CGAL QUIET COMPONENTS )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# include helper file
include( ${CGAL_USE_FILE} )
find_package (CGAL)
include (${CGAL_USE_FILE})
add_definitions (${CGAL_CXX_FLAGS_INIT})
include_directories (${CGAL_INCLUDE_DIRS})
set (libraries ${libraries} ${CGAL_LIBRARY} ${CGAL_3RD_PARTY_LIBRARIES})
set (CMAKE_EXE_LINKER_FLAGS "-dynamic ${CMAKE_EXE_LINKER_FLAGS}")
find_package (HDF5 QUIET COMPONENTS CXX)
if (HDF5_FOUND)
include_directories (SYSTEM ${HDF5_CXX_INCLUDE_DIR})
set (HDF5_libraries ${HDF5_hdf5_LIBRARY} ${HDF5_hdf5_cpp_LIBRARY})
set (HDF5_libraries hdf5 hdf5_cpp)
endif (HDF5_FOUND)
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# include for local directory
# include for local package
# Creating entries for target: exe
# ############################
add_executable( exe match.cpp )
add_to_cached_list( CGAL_EXECUTABLE_TARGETS exe )
# Link the executable to CGAL and third-party libraries
target_link_libraries(exe ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} ${libraries} ${HDF5_libraries})
I use CGAL and HDF5 together. Here's the relevant bit from my CMakeLists.txt:
find_package (HDF5 QUIET COMPONENTS CXX)
if (HDF5_FOUND)
include_directories (SYSTEM ${HDF5_CXX_INCLUDE_DIR})
add_library (hdf5 STATIC IMPORTED)
set_target_properties (hdf5 PROPERTIES IMPORTED_LOCATION ${HDF5_hdf5_LIBRARY})
add_library (hdf5_cpp STATIC IMPORTED)
set_target_properties (hdf5_cpp PROPERTIES IMPORTED_LOCATION ${HDF5_hdf5_cpp_LIBRARY})
set (HDF5_libraries hdf5 hdf5_cpp)
add_executable (exe match.cpp)
target_link_libraries (exe ${libraries} ${HDF5_libraries})
endif (HDF5_FOUND)
Earlier in CMakeLists.txt, there is:
find_package (CGAL)
include (${CGAL_USE_FILE})
add_definitions (${CGAL_CXX_FLAGS_INIT})
include_directories (${CGAL_INCLUDE_DIRS})
set (libraries ${libraries} ${CGAL_LIBRARY} ${CGAL_3RD_PARTY_LIBRARIES})
set (CMAKE_EXE_LINKER_FLAGS "-dynamic ${CMAKE_EXE_LINKER_FLAGS}")