How to apply a copy command in a CmakeLists file - c++

here is my CMakeLists.txt file which is exists in the current directory project1
in the project1 directory, I have a src directory which contains the source and headers files. I have the build directory.
cmake_minimum_required(VERSION 2.6)
#Declaration du projet
project(MYfirstcamke)
set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
#Inclusion de opencv
include_directories(/usr/local/include/)
link_directories(/usr/local/lib)
file(
GLOB_RECURSE
source_files
src/*
)
#declaration de l'executable
add_executable(
my_executable
${source_files}
)
#Configuration de l'edition de lien.
target_link_libraries(
my_executable opencv_core opencv_imgproc opencv_video opencv_highgui
)
before, the file command I need to copy the *.cpp and *.hpp in src directory that exist the current directory. I write this code:
file(GLOB MY_PUBLIC_HEADERS
"*.hpp"
)
file(GLOB MY_PUBLIC_SOURCE
"*.cpp"
)
file(COPY ${MY_PUBLIC_SOURCE} DESTINATION "src/")
file(COPY ${MY_PUBLIC_HEADERS} DESTINATION "src/")
when I start the cmake.. where the current directory is build. *.hpp and *.cpp are copied to src but the problem here is, src is also copied to build and cmake does not work in this case. I need that src rest in the project1 directory. How can I fix this problem?

Related

CMake can't find include file from subfolder

I want to add to my CMake project some subfolder which contains some additional functionality. MyProject is a shared library. Ideally it will be fine to just include files from subfolder and to compile all them together. But so far I've found a way only to include that as a static library that suits me too. So my files structure should look like the following:
MyProject
src
main.cpp
include
main.h
CMakeLists.txt
3dparty
SomeFolder
src
file.cpp
include
file.h
CMakeLists.txt
MyProject/CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(MyProject)
file(GLOB SRC . src/*.cpp)
file(GLOB INC . include/*.h)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
add_library(${PROJECT_NAME} SHARED ${SRC} ${INC})
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
add_subdirectory(3dparty/SomeFolder)
and 3dparty/SomeFolder/CMakeLists.txt:
project (SomeFolder)
file(GLOB SRC . src/*.cpp)
file(GLOB INC . include/*.h)
add_library(${PROJECT_NAME} STATIC ${SRC} ${INC})
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
I can compile the project and I see that compiler produses SomeFolder.a so it looks good. But for some reason I can't include files from 3dparty/SomeFolder/include in main.*. So far I've tried:
#include "file.h"
#include "3dparty/SomeFolder/file.h"
but nothing works. I get error: 'file.h' file not found.
What I did wrong? How can I include files from subproject?
Because you forgot to link the sublibrary to your main library. Usr target_link_libraries()

How to create Xcode groups with CMake? [duplicate]

I have following structure
Main (dir)
+-- CMakeLists.txt
+-- File.cpp
+-- File.hpp
+-- Dir (dir)
+-- CMakeLists.txt
+-- File1.cpp
+-- File1.hpp
+-- File2.cpp
+-- File2.hpp
Main/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED (VERSION 2.8.11)
PROJECT(Main)
FILE(GLOB SOURCE
"*.hpp"
"*.cpp"
)
ADD_SUBDIRECTORY(Dir)
ADD_EXECUTABLE(Main ${SOURCE})
Main/Dir/CmakeLists.txt
FILE(GLOB LOCAL_SOURCE
"*.hpp"
"*.cpp"
)
SET(SOURCE
${SOURCE}
${LOCAL_SOURCE}
PARENT_SCOPE
)
It generated the following structure in Visual Studio
What I want:
What I tried:
Main/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED (VERSION 2.8.11)
PROJECT(Main)
FILE(GLOB LOCAL_SOURCE
"*.hpp"
"*.cpp"
)
SET(SOURCE
${LOCAL_SOURCE}
)
ADD_SUBDIRECTORY(Dir)
SOURCE_GROUP(Main FILES ${LOCAL_SOURCE})
ADD_EXECUTABLE(Main ${SOURCE})
Main/Dir/CmakeLists.txt
FILE(GLOB LOCAL_SOURCE
"*.hpp"
"*.cpp"
)
SET(SOURCE
${SOURCE}
${LOCAL_SOURCE}
PARENT_SCOPE
)
SOURCE_GROUP(Dir FILES ${LOCAL_SOURCE})
What I get:
Please help me regarding this.
I do not want to use single CmakeFile.txt in Main directory having filters
Actual structure is many layers deep nesting structure. So please suggest the solution which will work for any level sub directory
There are several ready to use or adaptable solutions out there to mimic a Source Tree behavior like in Eclipse with CMake for Visual Studio (e.g. ADD_SRC_SUBFOLDER DESTINATION_SRCS from Zobra or GroupSources from Luca).
Here is my reduced version for your use case:
cmake_minimum_required(VERSION 2.8.10)
project(Main CXX)
set(
source_list
"File.cpp"
"File.hpp"
"Dir/File1.cpp"
"Dir/File1.hpp"
"Dir/File2.cpp"
"Dir/File2.hpp"
)
add_executable(Main ${source_list})
foreach(source IN LISTS source_list)
get_filename_component(source_path "${source}" PATH)
string(REPLACE "/" "\\" source_path_msvc "${source_path}")
source_group("${source_path_msvc}" FILES "${source}")
endforeach()
See the documentation of source_group() that you have to give the sub-directories with double backslashes.
For the reason why I replaced your file(GLOB ...) with a dedicated list of all source files I like to quote from CMake's file() command documentation:
We do not recommend using GLOB to collect a list of source files from
your source tree. If no CMakeLists.txt file changes when a source is
added or removed then the generated build system cannot know when to
ask CMake to regenerate.
And here is my fail-safe version (that checks for absolute paths) to be used as a function:
function(assign_source_group)
foreach(_source IN ITEMS ${ARGN})
if (IS_ABSOLUTE "${_source}")
file(RELATIVE_PATH _source_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${_source}")
else()
set(_source_rel "${_source}")
endif()
get_filename_component(_source_path "${_source_rel}" PATH)
string(REPLACE "/" "\\" _source_path_msvc "${_source_path}")
source_group("${_source_path_msvc}" FILES "${_source}")
endforeach()
endfunction(assign_source_group)
Which you would call in the example with
assign_source_group(${source_list})
As of CMake 3.8, the source_group command offers a TREE argument to recursively search the files paths of your sources, and structures the source groups to match your file system structure. Now, this offers a much cleaner solution:
project(Main)
set(SOURCE_LIST
"File.cpp"
"File.hpp"
"Dir/File1.cpp"
"Dir/File1.hpp"
"Dir/File2.cpp"
"Dir/File2.hpp"
)
add_executable(Main ${SOURCE_LIST})
# Create the source groups for source tree with root at CMAKE_CURRENT_SOURCE_DIR.
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_LIST})
I wanted to comment on https://stackoverflow.com/users/3987854/squareskittles 's answer, but I couldn't due to the lack of "reputation" ?
Anyway,
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_LIST})
works like a charm, but I also needed to set this:
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
I know that using the CMAKE glob function is usually frowned upon: Why is CMAKE glob evil, but in my case I found it to better than explicitly naming each file. I figured I would include a modified version of Florian's answer using GLOB.
# This code sorts the project files as they appear in the root directory
# Generate a list of all .c & .h files in the current directory and sub directores.
file(
GLOB_RECURSE source_list RELATIVE
"${CMAKE_CURRENT_SOURCE_DIR}"
*.c *.h
)
foreach(source IN LISTS source_list)
get_filename_component(source_path "${source}" PATH)
string(REPLACE "/" "\\" source_path_msvc "${source_path}")
source_group("${source_path_msvc}" FILES "${source}")
endforeach()
message(STATUS "Tree reorganized")

cmake only sub directories based library

I am trying to figure out to create a small cmake file. The directory struture includes 4 sub folders and which has src and header files. There is no src or header file at root. All I want is to create a Shared library using cmake by compiling these four folders.
CMakeLists.txt at root
cmake_minimum_required (VERSION 2.8.7)
Project(NEWPROJ)
set(${PROJECT_NAME}_MAJOR_VERSION 00)
set(${PROJECT_NAME}_MINOR_VERSION 01)
set(${PROJECT_NAME} 0)
set(${PROJECT_NAME}_VERSION
${${PROJECT_NAME}_MAJOR_VERSION}.${${PROJECT_NAME}_MINOR_VERSION}.${${PROJECT_NAME}_PATCH_VERSION})
set(${PROJECT_NAME}_SOVERSION ${${PROJECT_NAME}_MAJOR_VERSION}.${${PROJECT_NAME}_MINOR_VERSION})
Message ( Root )
include_directories("${PROJECT_SOURCE_DIR}/Folder1/include")
include_directories("${PROJECT_SOURCE_DIR}/Folder2/include")
include_directories("${PROJECT_SOURCE_DIR}/Folder3/include")
include_directories("${PROJECT_SOURCE_DIR}/Folder4/include")
add_subdirectory ("${PROJECT_SOURCE_DIR}/Folder1")
add_subdirectory ("${PROJECT_SOURCE_DIR}/Folder2")
add_subdirectory ("${PROJECT_SOURCE_DIR}/Folder3")
add_subdirectory ("${PROJECT_SOURCE_DIR}/Folder4")
add_library( ${PROJECT_NAME} SHARED ${SOURCE} ${HEADERS})
CMakeLists.txt in Folder1
MESSAGE(Folder1)
set(SOURCE
${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/src/file1.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/file2.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/file3.cc
PARENT_SCOPE
)
set(HEADERS
${HEADERS}
${CMAKE_CURRENT_SOURCE_DIR}/include/file1.h
${CMAKE_CURRENT_SOURCE_DIR}/include/file2.h
${CMAKE_CURRENT_SOURCE_DIR}/include/file3.h
${CMAKE_CURRENT_SOURCE_DIR}/include/file4.h
PARENT_SCOPE
)
cmake .. successful
make folder1/src/file1.cc:1:20: fatal error: file1.h: No such file or directory
file1.cc
#include "file1.h"
...

How to set Visual Studio Filters for nested sub directory using cmake

I have following structure
Main (dir)
+-- CMakeLists.txt
+-- File.cpp
+-- File.hpp
+-- Dir (dir)
+-- CMakeLists.txt
+-- File1.cpp
+-- File1.hpp
+-- File2.cpp
+-- File2.hpp
Main/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED (VERSION 2.8.11)
PROJECT(Main)
FILE(GLOB SOURCE
"*.hpp"
"*.cpp"
)
ADD_SUBDIRECTORY(Dir)
ADD_EXECUTABLE(Main ${SOURCE})
Main/Dir/CmakeLists.txt
FILE(GLOB LOCAL_SOURCE
"*.hpp"
"*.cpp"
)
SET(SOURCE
${SOURCE}
${LOCAL_SOURCE}
PARENT_SCOPE
)
It generated the following structure in Visual Studio
What I want:
What I tried:
Main/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED (VERSION 2.8.11)
PROJECT(Main)
FILE(GLOB LOCAL_SOURCE
"*.hpp"
"*.cpp"
)
SET(SOURCE
${LOCAL_SOURCE}
)
ADD_SUBDIRECTORY(Dir)
SOURCE_GROUP(Main FILES ${LOCAL_SOURCE})
ADD_EXECUTABLE(Main ${SOURCE})
Main/Dir/CmakeLists.txt
FILE(GLOB LOCAL_SOURCE
"*.hpp"
"*.cpp"
)
SET(SOURCE
${SOURCE}
${LOCAL_SOURCE}
PARENT_SCOPE
)
SOURCE_GROUP(Dir FILES ${LOCAL_SOURCE})
What I get:
Please help me regarding this.
I do not want to use single CmakeFile.txt in Main directory having filters
Actual structure is many layers deep nesting structure. So please suggest the solution which will work for any level sub directory
There are several ready to use or adaptable solutions out there to mimic a Source Tree behavior like in Eclipse with CMake for Visual Studio (e.g. ADD_SRC_SUBFOLDER DESTINATION_SRCS from Zobra or GroupSources from Luca).
Here is my reduced version for your use case:
cmake_minimum_required(VERSION 2.8.10)
project(Main CXX)
set(
source_list
"File.cpp"
"File.hpp"
"Dir/File1.cpp"
"Dir/File1.hpp"
"Dir/File2.cpp"
"Dir/File2.hpp"
)
add_executable(Main ${source_list})
foreach(source IN LISTS source_list)
get_filename_component(source_path "${source}" PATH)
string(REPLACE "/" "\\" source_path_msvc "${source_path}")
source_group("${source_path_msvc}" FILES "${source}")
endforeach()
See the documentation of source_group() that you have to give the sub-directories with double backslashes.
For the reason why I replaced your file(GLOB ...) with a dedicated list of all source files I like to quote from CMake's file() command documentation:
We do not recommend using GLOB to collect a list of source files from
your source tree. If no CMakeLists.txt file changes when a source is
added or removed then the generated build system cannot know when to
ask CMake to regenerate.
And here is my fail-safe version (that checks for absolute paths) to be used as a function:
function(assign_source_group)
foreach(_source IN ITEMS ${ARGN})
if (IS_ABSOLUTE "${_source}")
file(RELATIVE_PATH _source_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${_source}")
else()
set(_source_rel "${_source}")
endif()
get_filename_component(_source_path "${_source_rel}" PATH)
string(REPLACE "/" "\\" _source_path_msvc "${_source_path}")
source_group("${_source_path_msvc}" FILES "${_source}")
endforeach()
endfunction(assign_source_group)
Which you would call in the example with
assign_source_group(${source_list})
As of CMake 3.8, the source_group command offers a TREE argument to recursively search the files paths of your sources, and structures the source groups to match your file system structure. Now, this offers a much cleaner solution:
project(Main)
set(SOURCE_LIST
"File.cpp"
"File.hpp"
"Dir/File1.cpp"
"Dir/File1.hpp"
"Dir/File2.cpp"
"Dir/File2.hpp"
)
add_executable(Main ${SOURCE_LIST})
# Create the source groups for source tree with root at CMAKE_CURRENT_SOURCE_DIR.
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_LIST})
I wanted to comment on https://stackoverflow.com/users/3987854/squareskittles 's answer, but I couldn't due to the lack of "reputation" ?
Anyway,
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_LIST})
works like a charm, but I also needed to set this:
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
I know that using the CMAKE glob function is usually frowned upon: Why is CMAKE glob evil, but in my case I found it to better than explicitly naming each file. I figured I would include a modified version of Florian's answer using GLOB.
# This code sorts the project files as they appear in the root directory
# Generate a list of all .c & .h files in the current directory and sub directores.
file(
GLOB_RECURSE source_list RELATIVE
"${CMAKE_CURRENT_SOURCE_DIR}"
*.c *.h
)
foreach(source IN LISTS source_list)
get_filename_component(source_path "${source}" PATH)
string(REPLACE "/" "\\" source_path_msvc "${source_path}")
source_group("${source_path_msvc}" FILES "${source}")
endforeach()
message(STATUS "Tree reorganized")

CMake - dependencies (headers) between apps/libraries in same project

I have the following project structure:
CMakeLists.txt
lib1/CMakeLists.txt and all cpp and header files of the lib
lib2/CMakeLists.txt and all cpp and header files of the lib
app/CMakeLists.txt and all cpp and header files of the app
The main CMakeLists.txt looks like:
PROJECT( ${PROJECT_NAME} )
add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(app)
The lib1/CMakeLists.txt looks eg like (stripped):
SET(SOURCE
file.cpp
)
SET(HEADERS
some_lib_header.h
)
add_library( lib1 ${SOURCE} ${HEADERS} )
and the one for the app looks the same except of ADD_EXECUTABLE:
SET(SOURCE
main.cpp
)
SET(HEADERS
some_header.h
)
add_library( lib1 ${SOURCE} ${HEADERS} )
ADD_EXECUTABLE( app ${SOURCE} ${HEADERS} )
I found the setup working well this way because out of this, I can generate one Visual Studio solution file which contains all those three projects. But my problem is that my app includes header files of lib1 (and also of lib2, which depends on lib1). When I do
$mkdir build
$cd build
$cmake -C ..\myproject
it generates out-of-source VS .sln file as I want it, but the app doesn't compile because it can't find the header files of lib1 (obviously).
Now I read and tried many things, like TARGET_LINK_LIBRARIES( app lib1 ) (which got the app to link with the lib1, but not solve the header include issue), and things like add_subdirectory( ../lib1 ) in various variants in the CMakeLists.txt of app (which all throwed errors that I couldn't fix), and also find_package (which I guess is the wrong approach).
So how can I solve this (I guess simple...) problem?
Here's one possible solution:
Root CMakeLists.txt:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(${PROJECT_NAME})
add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(app)
lib1/CMakeLists.txt:
project(Lib1)
add_library(lib1 lib1.cpp lib1.h)
lib2/CMakeLists.txt:
project(Lib2)
add_library(lib2 lib2.cpp lib2.h)
# Add /lib1 to #include search path
include_directories(${Lib1_SOURCE_DIR})
# Specify lib2's dependency on lib1
target_link_libraries(lib2 lib1)
app/CMakeLists.txt:
project(App)
add_executable(app main.cpp some_header.h)
# Add /lib1 and /lib2 to #include search path
include_directories(${Lib1_SOURCE_DIR} ${Lib2_SOURCE_DIR})
# Specify app's dependency on lib2.
# lib2's dependency on lib1 is automatically added.
target_link_libraries(app lib2)
There are plenty of different ways to achieve the same end result here. For a relatively small project, I'd probably just use a single CMakeLists.txt:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(Test)
add_library(lib1 lib1/lib1.cpp lib1/lib1.h)
add_library(lib2 lib2/lib2.cpp lib2/lib2.h)
add_executable(app app/main.cpp app/some_header.h)
include_directories(${CMAKE_SOURCE_DIR}/lib1 ${CMAKE_SOURCE_DIR}/lib2)
target_link_libraries(lib2 lib1)
target_link_libraries(app lib2)
For further info on the relevant commands and their rationale, run:
cmake --help-command add_subdirectory
cmake --help-command include_directories
cmake --help-command target_link_libraries
Project
CMakeLists.txt
\-lib1
CMakeLists.txt
\- include \ lib1
\- src
\-lib2
CMakeLists.txt
\- include \ lib2
\- src
\-app
CMakeLists.txt
\- src
Suppose dependencies as follow:
lib1 ---> lib2 ---> app
\--------------> app
Something like this:
CMakeLists.txt:
add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(app)
lib1/CMakeLists.txt:
file(GLOB_RECURSE _HDRS "include/*.hpp")
file(GLOB_RECURSE _SRCS "src/*.[hc]pp")
add_library(lib1 ${_HDRS} ${_SRCS})
#target_link_libraries(lib1)
target_include_directories(lib1 PUBLIC include)
install(TARGETS lib1 DESTINATION lib)
install(FILES ${_HDRS} DESTINATION include/lib1)
lib2/CMakeLists.txt:
file(GLOB_RECURSE _HDRS "include/*.hpp")
file(GLOB_RECURSE _SRCS "src/*.[hc]pp")
add_library(lib2 ${_HDRS} ${_SRCS})
target_link_libraries(lib2 lib1)
target_include_directories(lib2 PUBLIC include)
install(TARGETS lib2 DESTINATION lib)
install(FILES ${_HDRS} DESTINATION include/lib2)
so in lib2/src/file.cpp you could do #include <lib1/header.hpp>
app/CMakeLists.txt:
file(GLOB_RECURSE _SRCS "src/*.[hc]pp")
add_executable(app ${_SRCS})
target_link_libraries(app lib1 lib2)
install(TARGETS app DESTINATION bin)
so in app/src/file.cpp you could do #include <lib1/header.hpp> and #include <lib2/header.hpp>
The magic is target_include_directories which attach the "include" directory to the target, so when linking with it you pull the include directory also ;)