CMake: custom add_executable failing when adding link libraries - cmake

I'm making a custom cross-platform library that needs to be in native gui mode, so, the add_executable need to have the parameter of WIN32 or MACOSX_BUNDLE depending on the platform. To facilitate this process I created a cmake function:
function(add_windowed_executable targetProject targetSources)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_executable(${targetProject} WIN32 ${targetSources})
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") # MacOS
add_executable(${targetProject} MACOSX_BUNDLE ${targetSources})
# ... link with native libraries
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux)
add_executable(${targetProject} ${targetSources})
# ... link with native libraries
endif()
endfunction()
Now when I try to create an executable with target_link_library it gives me the error "Cannot specify link libraries for target "test" which is not built by this project.". It comes down to the fact that the add_windowed_executable is the one that calls the add_executable and not the main file. Is there any way to use this function and allow to link with other libraries, basically replicating the add_executable command?
Main cmake
# ...
add_subdirectory("functions")
# ...
add_windowed_executable("test" "src/main.c")
target_link_libraries("test" "otherLibrary") # It's here where the error appears
EDIT: tryed with macro instead of function and the error still remains

add_executable(targetProject
You added targetProject. Not test. You want to use the variable:
add_executable(${targetProject} ${targetSources})
and the same with targetSources.

It needs to be done the oposite way. First check the os then create the function or macro:
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
function(add_windowed_executable targetProject)
add_executable(${targetProject} WIN32 ${ARGN})
endfunction()
# ...
endif ()

Related

cmake linker configuration for a project that uses wxWidgets

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})
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")

Include the CGAL shared libraries to deploy a CGAL program

I'm building a C++ program using CGAL, and I'm writing CMake install rules to deploy said program so that I can CPack the result and the end user doesn't have to install CGAL or any of its dependencies to use it. In order to do that, I need to include every shared library (DLLs on Windows etc) but I can't find a CMake variable that lets me do that. I searched around in the CGAL repo, but no luck. I tried using ${CGAL_LIBRARIES} but those don't give paths, and it doesn't seem like ${CGAL_LIBRARIES_DIRS} is a thing.
My current CMakeLists is based off the one generated by the dedicated CGAL script :
# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
project( MeshCleaner )
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
# CGAL and its components
find_package( CGAL QUIET COMPONENTS )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# 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_directories( BEFORE include )
# include for local package
# Creating entries for target: meshCleaner
# ############################
add_executable( ${PROJECT_NAME} main.cpp )
add_to_cached_list( CGAL_EXECUTABLE_TARGETS ${PROJECT_NAME} )
# Link the executable to CGAL and third-party libraries
target_link_libraries(${PROJECT_NAME} ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} )
install(TARGETS ${PROJECT_NAME} DESTINATION . COMPONENT Libraries)
message(${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES})
if(WIN32)
set(CMAKE_INSTALL_OPENMP_LIBRARIES TRUE)
include(InstallRequiredSystemLibraries)
endif()
include(${PROJECT_NAME}CPack)
The simplest way to "fix" that issue is to use CGAL as header-only. Please check the manual. Here is the direct link to the installation manual of CGAL-4.14, section "Header-only with CMake Configuration".

how to use framework using Cmake?

For Macos, I'd like to link to some framework. In windows, I would like to link to some library.
For example, OpenGL Framework, how to express this requirement using cmake?
You could try the following code:
target_link_libraries(<target name>
"-framework AVFoundation"
"-framework CoreGraphics"
"-framework CoreMotion"
"-framework Foundation"
"-framework MediaPlayer"
"-framework OpenGLES"
"-framework QuartzCore"
"-framework UIKit"
)
To tell CMake that you want to link to OpenGL, add the following to your CMakeLists.txt:
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
target_link_libraries(<your program name> ${OPENGL_LIBRARIES})
find_package will look for OpenGL and tell the rest of the script where OpenGL is by setting some OPENGL* variables. include_directories tells your compiler where to find OpenGL headers. target_link_libraries instructs CMake to link in OpenGL.
The following code will do different actions based on the operating system:
if(WIN32)
#Windows specific code
elseif(APPLE)
#OSX specific code
endif()
You could try the following macro code:
macro(ADD_OSX_FRAMEWORK fwname target)
find_library(FRAMEWORK_${fwname}
NAMES ${fwname}
PATHS ${CMAKE_OSX_SYSROOT}/System/Library
PATH_SUFFIXES Frameworks
NO_DEFAULT_PATH)
if( ${FRAMEWORK_${fwname}} STREQUAL FRAMEWORK_${fwname}-NOTFOUND)
MESSAGE(ERROR ": Framework ${fwname} not found")
else()
TARGET_LINK_LIBRARIES(${target} PUBLIC "${FRAMEWORK_${fwname}}/${fwname}")
MESSAGE(STATUS "Framework ${fwname} found at ${FRAMEWORK_${fwname}}")
endif()
endmacro(ADD_OSX_FRAMEWORK)
Example
ADD_OSX_FRAMEWORK(Foundation ${YOUR_TARGET}) # Add the foundation OSX Framework
You can find this example code here
For custom framework
cmake version 3.20.1
https://github.com/Sunbreak/cli-breakpad.trial/blob/master/CMakeLists.txt#L10-L12
if(APPLE)
find_library(BREAKPAD_CLIENT Breakpad "${CMAKE_CURRENT_SOURCE_DIR}/breakpad/mac/")
target_link_libraries(cli-breakpad PRIVATE ${BREAKPAD_CLIENT})

Project build configuration in CMake

My question is very similar to CMake : Changing name of Visual Studio and Xcode exectuables depending on configuration in a project generated by CMake. In that post the output file name will change according to the project configuration (Debug, Release and so on). I want to go further. When I know the configuration of the project, I want to tell the executable program to link different library names depending on project configurations. I was wondering whether there is a variable in CMake that can tell the project configuration. If there exists such a variable, my task will become easier:
if (Project_Configure_Name STREQUAL "Debug")
#do some thing
elseif (Project_Configure_Name STREQUAL "Release")
#do some thing
endif()
According to http://cmake.org/cmake/help/v2.8.8/cmake.html#command:target_link_libraries, you can specify libraries according to the configurations, for example:
target_link_libraries(mytarget
debug mydebuglibrary
optimized myreleaselibrary
)
Be careful that the optimized mode means every configuration that is not debug.
Following is a more complicated but more controllable solution:
Assuming you are linking to an imported library (not compiled in your cmake project), you can add it using:
add_library(foo STATIC IMPORTED)
set_property(TARGET foo PROPERTY IMPORTED_LOCATION_RELEASE c:/path/to/foo.lib)
set_property(TARGET foo PROPERTY IMPORTED_LOCATION_DEBUG c:/path/to/foo_d.lib)
add_executable(myexe src1.c src2.c)
target_link_libraries(myexe foo)
See http://www.cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets for more details.
There is always another way:
if(CMAKE_BUILD_TYPE MATCHES "release")
SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE})
else(CMAKE_BUILD_TYPE MATCHES "debug")
SET(CMAKE_BUILD_TYPE "debug")
endif(CMAKE_BUILD_TYPE MATCHES "release")
We can use the variable CMAKE_BUILD_TYPE. We can also change this variable at the beginning of invoking CMAKE:
cmake .. -DCMAKE_BUILD_TYPE:STRING=debug
Then we can use this variable as an indicator of build configuration.

Conditional add_custom_command in CMake

I am using a macro to create precompiled headers for my cmake project. For gcc, this macro uses add_custom_command to create a *.h.gch file which can then be added to the target along with the other source files with add_executable/add_library. The problem is that sometimes the same *.h.gch file is used for two different targets, because some libraries are built both as static and dynamic libs.
I need to call the macro after each of the add_library calls because for MSVC/Xcode, one needs to adjust the target properties to enable PCH usage/compilation. But for gcc, this results in an error as I'm trying to use add_custom_command with an output that already has a build rule (the .gch). Currently I am avoiding this error by just skipping the add_custom_command for any target that contains "Static" in its name - this happens to work because all the static libraries in the project have a "Static" postfix, but its obviously not a very elegant solution.
Is there a way in cmake to check if a target already has a build rule, or alternatively, a way to allow add_custom_command to fail silently without causing an error? Or is there a way to change my design so that I can avoid the problem entirely? I suppose one "solution" would be to add a conditional check in each of the CMakeLists, but I really don't want to do that.
This is the code I am currently using:
The Macro:
macro(SET_PRECOMPILED_HEADER targetName PCHFile)
if(MSVC)
# PCH for MSVC
elseif(${CMAKE_GENERATOR} MATCHES "Xcode")
# PCH for Xcode
else() #gcc
if(NOT ${targetName} MATCHES "Static") ## <-- this is bad
## set the correct "compilerArgs"
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PCHFile}.gch
COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1} ${compilerArgs}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PCHFile}
)
endif()
endmacro(SET_PRECOMPILED_HEADER targetName PCHFile)
...then in the CMakeLists, something like this:
# Dynamic version:
set(MODULE_NAME MyLib)
project(${MODULE_NAME})
## set ${sources}
add_library(${MODULE_NAME} SHARED ${sources} "src/precompiled.h.${PCH_EXT}")
set_target_properties(${MODULE_NAME} PROPERTIES COMPILE_DEFINITIONS MY_DLL_DEFINITION)
SET_PRECOMPILED_HEADER(${MODULE_NAME} "src/precompiled.h")
# Static version:
set(MODULE_NAME MyLibStatic)
project(${MODULE_NAME})
add_library(${MODULE_NAME} ${sources} "src/precompiled.h.${PCH_EXT}")
set_target_properties(${MODULE_NAME} PROPERTIES COMPILE_DEFINITIONS MY_STATIC_DEFINITION)
SET_PRECOMPILED_HEADER(${MODULE_NAME} "src/precompiled.h")
Thanks for your help! I'm sorry if this is a duplicate - there are already several questions on add_custom_command, but none of them quite seem to address what I'm after.
First, you can create target for each PCH and then use this before declaring new target:
if(TARGET ${PCHFile}.gch)
Another way:
In the root CMakeLists.txt:
set(PRECOMPILED_HEADERS "" CACHE INTERNAL "")
In the macro:
list(FIND PRECOMPILED_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/${PCHFile}.gch res)
if(NOT res EQUAL -1)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PCHFile}.gch
COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1} ${compilerArgs}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PCHFile}
)
list(APPEND PRECOMPILED_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/${PCHFile}.gch)
endif()