I am trying to add external library VTK in my project using cmake. What I have noticed is when I try to add this external library it silently add -std=gnu++11 but when I remove this third-party library that flag does not get added. Only flag I add is -std=c++17.
How I add external library is:
Tell cmake the place where I have installed vtk
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/vtk)
.. now link with vtk library
TARGET_link_libraries(
${target}
${VTK_LIRARIES}
)
Related
I am using CMake and trying to include libraries I use with the executable for distribution. One library I am using is boost for which I am using the statement
find_package(Boost REQUIRED thread filesystem)
This works fine, but I cannot redistribute to a computer where boost is not installed.
I then tried to use fetch_content but when cpack generates the installer, the library files built still aren't included.
set(BOOST_INCLUDE_LIBRARIES thread filesystem)
set(BOOST_ENABLE_CMAKE ON)
FetchContent_Declare(build_boost GIT_REPOSITORY https://github.com/boostorg/boost.git GIT_TAG boost-1.80.0)
FetchContent_GetProperties(build_boost)
if(NOT build_boost_POPULATED)
FetchContent_Populate(build_boost)
add_subdirectory(${build_boost_SOURCE_DIR} ${build_boost_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
How can I package the library files with the executable?
I am trying to build a cross-platform project using CMake and Visual C++ 2017 toolchain.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(CMakeLibTest)
add_executable(mainApp App.cpp)
target_include_directories(mainApp PRIVATE ${PROJECT_SOURCE_DIR}/../Lib)
target_link_libraries(mainApp -L${PROJECT_SOURCE_DIR}/../Win32/Debug -lLib)
Lib.lib is some static library. It is located in the folder ../Win32/Debug relative to the location of CMakeLists.txt and App.cpp.
When I start the project build I see strange options in the linker command line:
-LC:/Users/UserName/source/repos/CMakeLibTest/App/../Win32/Debug -lLib.lib
The linker cannot recognize these options and shows warnings:
warning LNK4044: unrecognized option '/LC:/Users/UserName/source/repos/CMakeLibTest/App/../Win32/Debug'; ignored
warning LNK4044: unrecognized option '/lLib.lib'; ignored
And finally it fails:
error LNK2019: unresolved external symbol "void __cdecl f(void)" (?f##YAXXZ) referenced in function main
Expected correct linker command line options:
/LIBPATH:"C:\Users\UserName\source\repos\CMakeLibTest\Win32\Debug\" "Lib.lib"
What I am doing wrong? What is the correct way to link libraries in CMake compatible with Visual Studio? Or maybe it is a bug in CMake?
#Tsyvarev is correct; there is no need to bother with the -l or -L flags when using target_link_libraries in CMake. The other linked question here proposes making the other DLL dependency an imported library. Here is what it would look like for your example:
# Add the static library 'Lib.lib', marking it as an IMPORT.
add_library(MyLib STATIC IMPORTED)
# Define the location of the library dependency.
set(MYLIB_FILE_PATH "C:/Users/UserName/source/repos/CMakeLibTest/Win32/Debug/Lib.lib")
# Tell CMake where to find the library.
set_target_properties(MyLib PROPERTIES IMPORTED_LOCATION ${MYLIB_FILE_PATH})
Note that if Win32 is your CMake build directory, you can just use CMAKE_BINARY_DIR or CMAKE_CURRENT_BINARY_DIR to get location of Lib.lib.
# Add the static library 'Lib.lib', marking it as an IMPORT.
add_library(MyLib STATIC IMPORTED)
# Tell CMake where to find the library.
set_target_properties(MyLib PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/Debug/Lib.lib)
I encourage checking out the Debug/Release options for IMPORTED_LOCATION, since you are using Visual Studio.
Now, you can use target_link_libraries to link the imported library:
target_link_libraries(mainApp MyLib)
An important note: If you used CMake to configure and build Lib.lib in the same source tree as mainApp, you can skip the IMPORTED steps, and simply call target_link_libraries as shown above.
I'm trying to include SFML as a dependency in my CMake project, and it compiles SFML fine, but I can't figure out how to link it.
Here's my CMakeLists.txt, with annotated comments:
cmake_minimum_required(VERSION 3.9.1)
set(CMAKE_LEGACY_CYGWIN_WIN32 1)
set (CMAKE_CXX_STANDARD 11) # Set C++11
project(CHIP8)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # static library
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # dynamic library
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # executables
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules" ${CMAKE_MODULE_PATH})
# Include SFML
add_subdirectory(${CMAKE_SOURCE_DIR}/dep/SFML)
include_directories(${CMAKE_SOURCE_DIR}/dep/SFML/include)
#Include where to find headers
include_directories(./src)
include_directories(./src/headers)
# Do a GLOB search, and add them to executable
set(EXECUTABLE_NAME "CHIP8")
file(GLOB SRC_FILES
"src/headers/*.h"
"src/*.cpp"
)
add_executable(${EXECUTABLE_NAME} ${SRC_FILES})
target_link_libraries(${EXECUTABLE_NAME} SFML) #Here is where the problem lies!
As you can see, I'm adding the subdirectory ./dep/SFML so it compiles with its own CMakeLists. Including the headers too on the next line, which VSCode picks up.
However, I'm having trouble on the last line: target_link_libraries. It can't find sfml SFML or SFML2. Therefore, I'm not sure how to link it.
Anyone have any idea?
Thanks.
SFML is not a single library, but a set of libraries, each of which corresponds to some component. For component named
<component-name>
the library target is named
sfml-<component-name>
When use SFML in your code, you should know which SFML component(s) you use (see SFML docs/tutorials), and link with corresponded libraries:
# Assuming you use "graphics" and "system" SFML components
target_link_libraries(<your-executable> sfml-graphics sfml-system)
Note, that such targets' naming is not officially documented, so it can be changed in the future.
Preferred way for link with SFML is to install it before (or during) configuring your project, and use
find_package(SFML REQUIRED COMPONENTS graphics system)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(<your-executable> ${SFML_LIBRARIES})
I have a library which is being generated as .a file (to be statically linked). There I want to include Qt framework (QtCore.framework) since I am on OSX. Is that possible? How could I do it using cmake?
My attempt:
In CMakeLists.txt I have
FIND_LIBRARY(QTCORE_LIBRARY NAMES QtCore
HINTS "${CMAKE_SOURCE_DIR}/osx/frameworks")
Then I print variable ${QTCORE_LIBRARY} and it gives right path.
Then in src/CMakeLists.txt (where my sources are) I link the library
TARGET_LINK_LIBRARIES(libname $${QTCORE_LIBRARY})
However when I launch the compilation it complains because it does not find
fatal error: 'QtGlobal' file not found
I have checked and QtCore.framework contains QtGlobal header
EDIT: In case someone has same problem I found solution.
I needed to add "${QTCORE_LIBRARY}/Headers" in my project include directories
Thanks in advance and regards
Why don't using the CMake "config.cmake" provided by Qt5 ?
something like:
# Qt Setting
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets)
...
target_link_libraries(libname Qt5::Core Qt5::Gui Qt5::Widgets)
CMake can find and use [...] Qt5 libraries. [...] Qt5 libraries are found using “Config-file Packages” shipped with Qt5
src: https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html
In order for find_package to be successful, Qt 5 must be found below the CMAKE_PREFIX_PATH, or the Qt5_DIR must be set in the CMake cache to the location of the Qt5WidgetsConfig.cmake file. The easiest way to use CMake is to set the CMAKE_PREFIX_PATH environment variable to the install prefix of Qt 5.
src: http://doc.qt.io/qt-5/cmake-manual.html
I'm trying to build libmcrypt library to use it with my project. It happened so, that they do not provide any kind of assistance.
First I've faced a problem with -no-undefined flag for gcc which is not further supported. I've replaced -no-undefined with -Wl,-no-undefined in makefiles and it does the trick.
But anyway I'am having problems. Lib is builded in .a files. I can see them in my C:\MinGW\msys\1.0\local\lib folder (it is analogue for /usr/local/lib folder in MinGW). But I need .dll library, not static .a .
So: what else must I change in makefiles to make MinGW build dll with header and debug info for it?