Move cmake release and debug folder - c++

I'm a beginner in Cmake and try to compile a library with my makefile. After hours and hours of research, all is good and compiles fines in visual studio. But it stills a problem for ouput folders.
I explain.
I want to have this structure of folders at the end :
-vc_2013_x64 < for platform x64 in release
--bin
---my.dll, my.lib, ...
-vc_2013_x64d < for platform x64 in debug
--bin
---my.dll, my.lib, ...
But when i try to make this tree, CMake add some "release" and "debug" folders after my bin folder. Like this following example :
-vc_2013_x64
--bin
*---release* --> NOT WANTED FOLDER
----my.dll, my.lib, ...
How i can fix it ? or try to overide like this maybe :
-release
--vc_2013_x64
---bin
----my.dll, my.lib, ...
I've search on web but have not find any answer.
Here is my CMakeLists.txt file :
cmake_minimum_required(VERSION 2.8)
#Configuration du projet
project(core CXX)
#add definitions
set(LIBRARY_OUTPUT_PATH /vc2013_x64/bin/)
set(LIBRARY_OUTPUT_DIRECTORY ../essai)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
add_definitions(-D_UNICODE -D_USRDLL -DCORE_EXPORTS)
add_definitions("/Gm")#"/GL" "/Gy"
#remove_definitions
add_definitions(-UCMAKE_INTDIR="Release")
#inclusions
#include_directories(include/*)
#file( GLOB_RECURSE source_files src/*)
#Configuration de la librairie
add_library(
core
SHARED
src/asserts.h
src/errors.h
src/filepath.h
src/memory.h
src/resources.h
src/stdafx.h
src/streams.h
src/string.h
src/system.h
src/variants.h
src/filepath.cpp
src/main.cpp
src/memory.cpp
src/resources.cpp
src/stdafx.cpp
src/streams.cpp
src/string.cpp
src/system.cpp
)
And here is my structure folder :
-core
--src -> contains my .h and .cpp files
--win32 -> solution files of visual studios
--cmake_build -> contains my files generated by cmake
--CMakeLists.txt
Thanks
Edit :
So i try to make something like you say, but it doesn't create folder i specifiy :
project(core CXX)
#add definitions
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
add_definitions(-D_UNICODE -D_USRDLL -DCORE_EXPORTS)
add_definitions("/Gm")#"/GL" "/Gy"
#remove_definitions
add_definitions(-UCMAKE_INTDIR="Release")
#Configuration de la librairie
add_library(
core
SHARED
src/asserts.h
src/errors.h
src/filepath.h
src/memory.h
src/resources.h
src/stdafx.h
src/streams.h
src/string.h
src/system.h
src/variants.h
src/filepath.cpp
src/main.cpp
src/memory.cpp
src/resources.cpp
src/stdafx.cpp
src/streams.cpp
src/string.cpp
src/system.cpp
)
set(DIR_NAME "/vc2013_x64")
set(LIBRARY_OUTPUT_PATH_RELEASE "${DIR_NAME}/bin")
set(LIBRARY_OUTPUT_PATH_DEBUG "${DIR_NAME}d/bin")
install (TARGETS core DESTINATION ${LIBRARY_OUTPUT_PATH_RELEASE} CONFIGURATIONS Release)
install (TARGETS core DESTINATION ${LIBRARY_OUTPUT_PATH_DEBUG})

So i found my answer by myself. I think i've not explain correctly for my first question. It was for just remove the folder after build. The following commands are working :
set(BIN_PATH "../path") -> define your principal path
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" ) ->define path for archive
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" ) ->define path for Library
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" ) ->define path for Runtime
Normally, all the files are created in /bin.

What you could use is the install target of CMAKE. It allows you to define a install target where you can copy your build libraries and dependent files to. For your example this could look something like this:
add_library(core SHARED
...
)
set(DIR_NAME "/vc2013_x64")
set(LIBRARY_OUTPUT_PATH_RELEASE "${DIR_NAME}/bin")
set(LIBRARY_OUTPUT_PATH_DEBUG "${DIR_NAME}d/bin")
install (TARGETS core DESTINATION ${LIBRARY_OUTPUT_PATH_RELEASE} CONFIGURATIONS Release)
install (TARGETS core DESTINATION ${LIBRARY_OUTPUT_PATH_DEBUG} CONFIGURATIONS Debug)
You can then just run the install target in your build environment and the files are copied where you want them to be.

Related

CMake Linking Header Only Library (Working Example) for which I want explanation

I am learning how to use ExternalProject to download header only libaries and link to my executable.
My workflow is following:
I download header library Eigen using ExtenalProject:
cmake -DGET_LIBS=ON -DBUILD_SHARED_LIBS=ON -DBUILD_MY_PROJECTS=OFF -G
"Visual Studio 17 2022" -A x64 .. && cmake --build . --config Release
Then I run the same CMakeLists a second time, but this time I disable ExternalProject and compile the executable that links the Eigen:
cmake -DGET_LIBS=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_MY_PROJECTS=ON
-G "Visual Studio 17 2022" -A x64 .. && cmake --build . --config Release
Question
Why I need to use both of these commands since in target_include_directories command I specify the same path as in include_directories?
In the code below I need two commands include_directories and target_include_directories.
I thought that it would be enough to use only target_include_directories, but without include_directories it wont work.
if (BUILD_MY_PROJECTS)
add_executable(my_exe main.cpp)
include_directories("${CMAKE_BINARY_DIR}/install/eigen/") #add directory of the header-only library without this the next line wort work
target_include_directories(my_exe INTERFACE "${CMAKE_BINARY_DIR}/install/eigen/")# link exe to exectable
endif ()
My full CMakeLists.txt is following:
project(superbuild LANGUAGES CXX)
cmake_minimum_required(VERSION 3.19)
########################################################################
# EIGEN
########################################################################
SET(GET_LIBS "" CACHE STRING "Set option to download dependencies")
if (GET_LIBS)
message(AUTHOR_WARNING ${GET_LIBS})
ExternalProject_Add(eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG 3.4.0
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/install/eigen #this does nothing...
SOURCE_DIR "${CMAKE_BINARY_DIR}/install/eigen" #install in my local build dir
#SOURCE_DIR ${CMAKE_INSTALL_PREFIX}
BUILD_COMMAND "" #do not build
INSTALL_COMMAND "" #do not install
)
endif ()
###############################################################################
#EXECUTABLES
###############################################################################
if (BUILD_MY_PROJECTS)
add_executable(my_exe main.cpp)
include_directories("${CMAKE_BINARY_DIR}/install/eigen/") #add directory of the header-only library without this the next line wort work
target_include_directories(my_exe INTERFACE "${CMAKE_BINARY_DIR}/install/eigen/")# link exe to exectable
endif ()
Tl;dr never use include_directories. Ever. Learn how property visibility works in CMake instead.
include_directories("${CMAKE_BINARY_DIR}/install/eigen/") #add directory of the header-only library without this the next line wort work
target_include_directories(my_exe INTERFACE "${CMAKE_BINARY_DIR}/install/eigen/")# link exe to exectable
In the code you supplied, include_directories is implicitly setting the INCLUDE_DIRECTORIES property on your my_exe target. Then the second line sets the INTERFACE_INCLUDE_DIRECTORIES property.
INCLUDE_DIRECTORIES is a list of include directories that must be passed to the compiler when building the target. INTERFACE_INCLUDE_DIRECTORIES is a list of include directories that must be passed to the compiler when building targets that link to this one. This doesn't make much sense for an executable
Slightly better would be to write:
target_include_directories(my_exe PRIVATE "${CMAKE_BINARY_DIR}/install/eigen/")
This will just populate INCLUDE_DIRECTORIES without touching the INTERFACE_ version. If you wanted both you could write PUBLIC instead of PRIVATE or INTERFACE. By definition, PUBLIC is just both of the others.
But this isn't a great dependency management strategy anyway... digging into the source tree guts of a project isn't scalable. It's also difficult to try different versions of Eigen without editing your build files.
I would just write:
cmake_minimum_required(VERSION 3.23)
project(example)
find_package(Eigen3 REQUIRED)
add_executable(my_exe main.cpp)
target_link_libraries(my_exe PRIVATE Eigen3::Eigen)
Use a proper package manager like vcpkg or Conan to handle downloading Eigen when it isn't available on the system.

CMake import local shared library and how to include it in a header

I'm stuck to import a shared library in another project with CMake.
my-utils-lib
My lib files are generated in my-utils-lib project:
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(my-utils-lib VERSION 1.0.0 DESCRIPTION "Utils for C++.")
set(CMAKE_CXX_STANDARD 14)
add_library(
my-utils-lib SHARED
./src/string_utils/find_matches.cpp
./src/string_utils/split.cpp
./src/string_utils/format.cpp
./src/vector_utils/print_vector.cpp
)
set_target_properties(
my-utils-lib PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION 1
)
target_include_directories(my-utils-lib PRIVATE src)
include(GNUInstallDirs)
install(
TARGETS my-utils-lib
DESTINATION ${CMAKE_INSTALL_LIBDIR}
EXPORT my-utils-lib
)
configure_file(my-utils-lib.pc.in my-utils-lib.pc #ONLY)
install(
FILES ${CMAKE_BINARY_DIR}/my-utils-lib.pc
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig
)
I run mkdir build && cd ./build/ && cmake .. && sudo make install to generate and install my library files.
Finally, my-utils-lib project tree looks like this:
CMakeLists.txt
my-utils-lib.pc.in
src/
|_main.cpp
|_main.h
|_string_utils/
|_find_matches.cpp
|_find_matches.h
|_format.cpp
|_format.h
|_split.cpp
|_split.h
|_vector_utils/
|_print_vector.cpp
|_print_vector.h
my-project
In another project, I'd like to import "split.h" in one of my local headers without having to enter the full path to the actual file. Something like:
#include "my-utils-lib/string_utils/split.h"
I tried many solution so far, none of them is working. My last attempt is:
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(longer_sub_sequence)
set(CMAKE_CXX_STANDARD 14)
find_package(Qt5Widgets REQUIRED)
find_library(my-utils-lib libmy-utils-lib.1.0.0.dylib)
add_library(my-utils-lib SHARED IMPORTED)
add_executable(
longer_sub_sequence
main.cpp
visualization/draw_matrix.cpp
visualization/draw_matrix.h
)
target_link_libraries(longer_sub_sequence my-utils-lib Qt5::Widgets)
CMake autobuild runs fine (I am using CLion as an IDE) but I can't find how to import any code from my library.
I checked in /usr/local/lib (where CMake generated my lib files), and found the correct lib files :
find . -maxdepth 1 -name "*my-utils-lib*" -print
./libmy-utils-lib.1.0.0.dylib
./libmy-utils-lib.1.dylib
./libmy-utils-lib.dylib
What am I missing here ?
You are missing to install the library header.
You should add an install command for header just like you did with the library binary.
Moreover you should consider using comparison signs (< and >) instead of double quote for include directive :
#include <my-utils-lib/string_utils/split.h>
One last remark, you are generating and installing a .pc file which is used by pkg-config only and you're not using it.

How to install only one executable with make install?

I have a project with CMakeLists.txt and 7 executables with it:
/Project
/build
/Subprogram1
/Subprogram2
...
/Subprogram7
CMakeLists.txt
My CMakeLists.txt :
project(Project)
cmake_minimum_required(VERSION 2.8)
set( CMAKE_CXX_FLAGS "-O0 -Wall -pedantic -std=c++11" )
include_directories( "${PROJECT_SOURCE_DIR}/headers" )
include_directories( "${PROJECT_SOURCE_DIR}/Subprogram1/headers" )
include_directories( "${PROJECT_SOURCE_DIR}/Subprogram2/headers" )
include_directories( "${PROJECT_SOURCE_DIR}/Subprogram3/headers" )
....
include_directories( "${PROJECT_SOURCE_DIR}/Subprogram7/headers" )
set( INSTALL_PATH /usr/local/bin/ )
set( INSTALL_MANPAGES_PATH /usr/local/man/man1 )
add_executable(Subprogram1
"${PROJECT_SOURCE_DIR}/headers/headers.h"
"${PROJECT_SOURCE_DIR}/Subprogram1/headers/headers1.cpp"
"${PROJECT_SOURCE_DIR}/Subprogram1/src/main.cpp")
....
add_executable(Subprogram7
"${PROJECT_SOURCE_DIR}/headers/headers.h"
"${PROJECT_SOURCE_DIR}/Subprogram7/headers/headers7.cpp"
"${PROJECT_SOURCE_DIR}/Subprogram7/src/main.cpp")
install( TARGETS Subprogram1 DESTINATION ${INSTALL_PATH} )
...
install( TARGETS Subprogram7 DESTINATION ${INSTALL_PATH} )
install( FILES "${PROJECT_SOURCE_DIR}/manpages/Subprogram1.1" DESTINATION ${INSTALL_MANPAGES_PATH})
...
install( FILES "${PROJECT_SOURCE_DIR}/manpages/Subprogram7.1" DESTINATION ${INSTALL_MANPAGES_PATH})
Well I want to build and install only ONE Subprogram3. In folder /build I typed:
cmake ../
make Subprogram3
make install
But Cmake and Make util made ALL programs and installed them. I want only one Subprogram3. How to make it? And I want to save my CMakeLists.txt immutable and only find needed commandline parameters to cmake or make utils. How to do it?
Installation of CMake project always processes all install() invocations in CMakeLists.txt. One cannot ask CMake to install only a specific file.
Also, installation phase depends from the ALL target as whole. CMake doesn't create dependency between installed files (or targets) and commands which creates these files (or targets).
So, if you plan to build and install only a selected file/target, your scripts should issue commands which create only this file/target, and issue install only for given file/target. This can be achieved by, e.g., an option you passed to cmake.
Things are quite different, if the project is not installed but packed (using cpack).
Still installation phase is completely performed, and it still depends from ALL as whole.
But it is possible to distribute files, install-ed in the project, into several packages. So one of this packages will contain only a specific file. This is achieved by assigning different COMPONENT options for install command.

CMAKE: How do I install executable along with required dylib to custom directory?

So I'm using CMake to build a C++ project (on Mac OS) and my project relies on a dylib (I'm using TBB https://www.threadingbuildingblocks.org/ but the specific library itself doesn't matter)
If I do a standard "cmake" and "make" it builds the executable where I want it and when I run my app, the dylib links correctly and everything works perfectly.
The problem comes in when I try to do a "make install" and try to run the resulting executable from the install directory. I get an "image not found" error:
dyld: Library not loaded: #rpath/libtbb.dylib
Referenced from:
/Users/MyName/Desktop/ProjectRoot/install/./MyApp
Reason: image not found
Interestingly, if I do a regular "make" without an install, and then manually copy over the executable to the install directory, then that will link against my dylib properly. I have no idea why that is.
My directory structure is as follows:
Root
CMakeLists.txt
Source/
Libraries/
tbb/
include/
lib/
libtbb.dylib
install/
...and my CMakeLists.txt file is below:
# Start of CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project (MyApp)
# Set C++ version and output paths
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/install")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
# Find TBB
find_library (
TBB_LIBRARIES
NAMES tbb libtbb # what to look for
HINTS "${CMAKE_SOURCE_DIR}/Libraries/tbb/lib" # where to look
NO_DEFAULT_PATH # do not search system default paths
)
# Set Custom Include Files + TBB header files
include_directories(Source/Headers Libraries/tbb/include)
# Set Source Files
file(GLOB_RECURSE SRC_FILES "Source/*.cpp")
add_executable(MyApp ${SRC_FILES})
# Link Libraries
target_link_libraries(MyApp ${TBB_LIBRARIES})
# Set compile flags
set_target_properties(MyApp PROPERTIES CXX_STANDARD 14) #LINK_FLAGS "-Wl")
target_compile_features(MyApp PUBLIC cxx_std_14)
# Install executable
install(TARGETS MyApp DESTINATION .)
If I try to also add the following line, and install the dylib as well:
install(TARGETS ${TBB_LIBRARIES} DESTINATION lib)
then when I do a "make install" I get the following error instead:
install TARGETS given target
"/Users/MyName/Desktop/ProjectRoot/Libraries/tbb/lib/libtbb.dylib"
which does not exist in this directory.
So I just can't seem to get this install to work. How do I fix it so that both my executable and my library get installed in the right place and that my executable will be able to link against my library when run?

QtCreator CMake Project Auto-Complete Not Always Working?

I have been working on a project that uses cmake as the build system and working in QtCreator just fine. There are sub-projects (some Qt related and others just plain c++) and up until now all of them were able to find header files and hence provide auto-complete functionality.
At some point, I added a Qt project and noticed that my headers for Qt proejcts were not being found by the editor. The really odd thing is they compile and run just fine via QtCreator 'Build->Build All'. It is almost like QtCreator just can't find them for auto-complete.
I looked around and users had problems in earlier versions of QtCreator finding Qt classes, but did not seem to have a problem finding their defined header files.
I put together a simplified "project" and added two fresh Qt Widget Application projects with the main window being a QDialog instead of QMainWindow. I added the following CMakeLists.txt file to each project:
My Standard CMake File For Qt
cmake_minimum_required(VERSION 3.0.2)
project("FooProj")
set(EXECUTABLE_NAME "Foo")
# Find Qt packages.
find_package(Qt5Core)
find_package(Qt5Widgets)
find_package(Qt5Gui)
set(CMAKE_AUTOMOC ON)
set(SOURCES
"src/main.cpp"
"src/Dialog.cpp"
)
set(HEADERS
"includes/Dialog.h"
)
set(UI_FORMS
"forms/Dialog.ui"
)
set(RESOURCES_FILES
"resources/resources.qrc"
)
# Convert Qt UI and resource files to C/C++ files.
qt5_wrap_ui(UI_HEADERS ${UI_FORMS})
qt5_add_resources(RESOURCES ${RESOURCES_FILES})
# Define the executable to be built.
add_executable(${EXECUTABLE_NAME}
${SOURCES}
${HEADERS}
${UI_HEADERS}
${RESOURCES}
)
# Include paths for this project.
target_include_directories(${EXECUTABLE_NAME} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
"${CMAKE_CURRENT_BINARY_DIR}"
)
# Link to appropriate libraries.
target_link_libraries(${EXECUTABLE_NAME}
Qt5::Core
Qt5::Widgets
Qt5::Gui
)
# Set distribution location and project structure.
set(DIST_DIR "${CMAKE_BINARY_DIR}/dist/${EXECUTABLE_NAME}")
set_property(TARGET ${EXECUTABLE_NAME} PROPERTY FOLDER "Guis")
# Setup distribution environment and copy final executable.
add_custom_command(TARGET ${EXECUTABLE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${DIST_DIR}
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${EXECUTABLE_NAME}> ${DIST_DIR}
)
# OS specific deployment setup for sytems without Qt installed.
if(WIN32)
add_custom_command(TARGET ${EXECUTABLE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/QtDeploymentFiles/run.bat" ${DIST_DIR}/${EXECUTABLE_NAME}.bat
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/QtDeploymentFiles/qt.conf" ${DIST_DIR}
)
elseif(UNIX AND NOT APPLE)
add_custom_command(TARGET ${EXECUTABLE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/QtDeploymentFiles/run.sh" ${DIST_DIR}/${EXECUTABLE_NAME}.sh
)
endif()
Along with this top level CMakeLists.txt:
Top Level CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project("Test")
add_subdirectory("FooProj")
add_subdirectory("BarProj")
Everything makes, compiles, and runs just fine. Auto-complete also works.
I add a Qt library project I need and everything still works fine even when I use the library files. Here is the cmake file for my library:
Qt Library CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project("QtLib")
set(LIB_NAME "QtLib")
# Find Qt packages.
find_package(Qt5Core)
find_package(Qt5Widgets)
set(CMAKE_AUTOMOC ON)
# Set source files to be built.
set(SOURCES
"src/LibFile1.cpp"
"src/LibFile2.cpp"
.
.
.
)
set(HEADERS
"includes/LibFile1Header.h"
"includes/LibFile2Header.h"
"includes/OtherHeaders.h"
.
.
.
)
add_library(${LIB_NAME} ${SOURCES} ${HEADERS})
set_property(TARGET ${LIB_NAME} PROPERTY FOLDER "Libs")
target_include_directories(${LIB_NAME} PRIVATE
"${Qt5Core_INCLUDE_DIRS}"
"${Qt5Widgets_INCLUDE_DIRS}"
"${CMAKE_CURRENT_BINARY_DIR}"
)
target_include_directories(${LIB_NAME} PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
)
target_link_libraries(${LIB_NAME}
Qt5::Core
Qt5::Widgets
)
Lastly I add one of my real projects (one of the ones that does not auto-complete) The structure is as follows:
-forms
-MainForm.ui
-resources
-resources.qrc
-resource files and dirs
-src
-main.cpp
-MainWindow.cpp
-includes
-MainWindow.h
The CMake file is exactly the same as the ones above. Everything makes, compiles, and runs just fine, but auto-complete stops working. I also noticed that ui_MainWindow.h (the one being auto-generated in my project binary directory) is not found.
The really odd thing is, if I just the exact same files with no changes and just open it as a stand alone project, auto-complete works.
Is there something off about my cmake files or is this a Qt related issue? The same thing happens in VS2013 which makes me think it is not QtCreator related, but I am not sure.
After reading around a little more, it seemed like this might be a QtCreator issue (although I don't know why I had problems in VS2013 when I first tested it). I ended up upgrading my QtCreator to 3.6 and my auto-complete problem went away.