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})
Related
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 ()
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")
I build llvm from git and want to use the libraries in a project, especially the libclang.
The "makefiles" are generated by means of CMake and for the LLVM part I found the setting LLVM_DIR to reroute the path for the llvm libraries, but for Clang I cannot find such a variable and I still see in my link line (it is a Cygwin system):
/usr/lib/libclang.dll.a /usr/lib/libclangTooling.dll.a.
Question: which environment variable do I set to get the right build Clang libraries?
The variable is Clang_DIR.
Just in case, I attach a minimalistic example of CMakeLists.txt file as well.
cmake_minimum_required(VERSION 3.12)
# Find CMake file for Clang
find_package(Clang REQUIRED)
# Add path to LLVM modules
set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
"${LLVM_CMAKE_DIR}"
)
# import LLVM CMake functions
include(AddLLVM)
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CLANG_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_definitions(${CLANG_DEFINITIONS})
add_llvm_executable(myTool main.cpp)
set_property(TARGET myTool PROPERTY CXX_STANDARD 11)
target_link_libraries(myTool PRIVATE clangTooling)
I've been developing a program with Qt5 on Linux without a problem, now I'm trying to compile it on Windows using CMake (with Qt5CoreConfig.cmake, etc.) and mingw32.
Everything works OK, except that I don't know how to static link my project.
This is how I add Qt5 to my project:
foreach(lib Qt5Core Qt5Widgets Qt5Gui Qt5Xml)
find_package(${lib} REQUIRED)
include_directories(${${lib}_INCLUDE_DIRS})
list(APPEND LIBS ${${lib}_LIBRARIES})
add_definitions(${${lib}_DEFINITIONS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${${lib}_EXECUTABLE_COMPILE_FLAGS}")
endforeach(lib)
What should I do to make it static?
You cannot build a static application with the windows version installed from the QT web site. You need to compile a windows binary from source with -static option enabled.
I am trying to add xapian search engine library in cmake file
project(search)
cmake_minimum_required(VERSION 2.8)
find_package(Xapian REQUIRED)
aux_source_directory(. SRC_LIST)
target_link_libraries(${PROJECT_NAME}
${Xapian_LIBRARY}
)
add_executable(${PROJECT_NAME} ${SRC_LIST})
This is not working can any one tell me how to add this if i compile with -lxapian it works
Swap target_link_libraries() and add_executable() calls. You can link library only to already defined target.
And use ${XAPIAN_LIBRARIES} instead of ${Xapian_LIBRARY}.