I have a directory layout like the following
projectA/
|-- CMakeLists.txt
|-- src/
|-- main.cpp
projectB/
|-- CMakeLists.txt
|-- src/
|-- file1.cpp
|-- file1.hpp
|-- file2.hpp
|-- main.cpp
|-- third_party/
|-- include
|-- lib1
I opened both project successfully in Qt Creator (using Ctrl+O and open the CMakeLists.txt file) and they're are able to build and run independently.
I need to gain access to file1.Xpp and file2.hpp from projectA. Is there a way in Qt Creator to add projectB as a subproject in projectA? And One might keep in mind that file1.Xpp and file2.hpp might depend on the third party library.
Using Ctrl+N -> Other Project -> Subdirs Project I can add a subproject, but only an empty one, if I'm not mistaken.
No, you can't add a CMake subject as a sub project.
Sub project is based of qmake.
Related
I have a project with multiple subfolders under my include folder. For instance:
include/
|-- foo/
|-- foo_header1.h
\-- foo_header2.h
|-- bar/
|-- bar_header1.h
\-- bar_header2.h
|-- root_header1.h
\-- root_header2.h
src/
|-- foo/
|-- foo_source1.cpp
\-- foo_source2.cpp
|-- bar/
|-- bar_source1.cpp
\-- bar_source2.cpp
|-- root_source1.cpp
|-- root_source2.cpp
\-- main.cpp
Curently my Makefile is:
all:
g++ -Iinclude/ -Iinclude/foo -Iinclude/bar -o main src/*.cpp src/foo/*.cpp src/bar/*.cpp
I want a way to make that command "smarter", so I don't hardcode all the folders in my source and include directories (potentially my project will have many folders). I just want some way to specify the root header directory and make g++ find the files recursively.
Does anyone know if I can do that? Maybe using the makefile or some special flag of g++...
You could just add "-Iinclude" to g++ and include the headers as following
#include <foo/foo_header1.h>
#include <bar/bar_header2.h>
This also eliminates the need to have the foo/bar prefix in the header filename. After renaming, the above simplifies to follows
#include <foo/header1.h>
#include <bar/header2.h>
I decided to try CMake for my new project. I've spent some hours reading the official documentation and tutorials so I've gained some basic knowledge, but unable to write proper CMakeLists for my project.
The file hierarchy looks like this:
project
|-- library
| |-- CMakeLists.txt
| |-- lib.cpp
| `-- lib.h
|-- app1
| |-- CMakeLists.txt
| |-- app1.cpp
| |-- app1.h
| `-- main.cpp
|-- app2
| |-- CMakeLists.txt
| |-- app2.cpp
| |-- app2.h
| `-- main.cpp
`-- app3
|-- CMakeLists.txt
|-- app3.cpp
|-- app3.h
`-- main.cpp
The point is that I want to compile the library as a static library so it can be used by app1, app2, app3 and so on. The reason for this structure is that each appN has it's own main function that will be run on AWS Lambda, but I'd like to write some common code shared between them in the library.
From what I've read so far it's easy to add a library if it was in a sub-directory of the appN using add_directory() and add it to the target_link_libraries(), but I cannot figure out how to add a library that is outside of the appN folders. I tried using relative paths like ${CMAKE_CURRENT_SOURCE_DIR}/../library/lib.a etc, but then CMake gives error that library is outside project folder.
I'm sure there is some way to do this. I've read some about CMake modules and find_package(), but it seems to be a bit overkill/complicated for this use-case, I think?
Wouldn't something like this work in a main CMakeLists.txt (where you create the PROJECT)?
ADD_LIBRARY(MyLib STATIC library/lib.cpp library/lib.h)
ADD_EXECUTABLE(app1 STATIC app1/app1.cpp app1/app1.h)
ADD_EXECUTABLE(app2 STATIC app2/app2.cpp app2/app2.h)
ADD_EXECUTABLE(app3 STATIC app3/app3.cpp app3/app3.h)
TARGET_LINK_LIBRARIES(app1 MyLib)
TARGET_LINK_LIBRARIES(app2 MyLib)
TARGET_LINK_LIBRARIES(app3 MyLib)
I ended up creating a main CMakeLists.txt that includes the subdirectories which again has their CMakeLists.txt. I found this approach to be the cleanest.
If someone else come by this I've included my complete CMakeLists.txt files below for reference along with the updated file hierarchy.
File hierarchy
project
|-- CMakeLists.txt
|-- library
| |-- CMakeLists.txt
| |-- lib.cpp
| `-- lib.h
|-- app1
| |-- CMakeLists.txt
| |-- app1.cpp
| |-- app1.h
| `-- main.cpp
|-- app2
| |-- CMakeLists.txt
| |-- app2.cpp
| |-- app2.h
| `-- main.cpp
`-- app3
|-- CMakeLists.txt
|-- app3.cpp
|-- app3.h
`-- main.cpp
CMakeLists.txt
project(MyProject)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(library)
add_subdirectory(app1)
add_subdirectory(app2)
add_subdirectory(app3)
library/CMakeLists.txt
project(library)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
add_library(${PROJECT_NAME} STATIC lib.cpp lib.h)
target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
app1/CMakeLists.txt
project(app1)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
add_executable(${PROJECT_NAME} main.cpp app1.cpp app1.h)
target_link_libraries(${PROJECT_NAME} library)
app2/CMakeLists.txt
project(app2)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
add_executable(${PROJECT_NAME} main.cpp app2.cpp app2.h)
target_link_libraries(${PROJECT_NAME} library)
app3/CMakeLists.txt
project(app3)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
add_executable(${PROJECT_NAME} main.cpp app3.cpp app3.h)
target_link_libraries(${PROJECT_NAME} library)
I want to avoid build twice all sources for my test... I have the following project tree:
|-- my_executable
|-- CMakeFiles.txt
|-- resources
|-- include
|-- src
|-- CMakeFiles.txt
|-- *.cpp
|-- test
|-- CMakeFiles.txt
|-- test.cpp
The problem is that for building test I need the same sources of my_executable and cmake build them twice.
The build time this way is double.
Can I do better?
What I have tried:
Have an OBJECT library and then use the *.obj files as input of both test and application.
But the problem is that this "objects" library has some dependencies, cpprestsdk, boost and others... I'm unable to set correctly the include dir for this target library :(
You can add a library target and link both your main executable and test code with that library. That way the library is built once and used many times.
I'm trying to use cmake for the first time and I'm having a hard time getting this to work. There's a source file and a library file (Lab_4.cpp and Trip_4.cpp, respectively) and they're both in the source folder (Lab_4). Here's what's in my cmake file so far:
cmake_minimum_required (VERSION 2.6)
project (Lab_4)
#add executable
add_executable(Lab_4 ${PROJECT_SOURCE DIR}/Lab_4.cxx)
target_link_libraries (Lab_4 ${EXTRA_LISTS})
#add libraries
add_library (${PROJECT_SOURCE_DIR}/Trip.cxx)
ls shows both files are in that folder. I'm really new to cmake so I'm sure I'm making an obvious mistake but I have no idea what it is. Any help would be appreciated!
cmake_minimum_required(VERSION 2.6)
project(Lab_4)
add_library(Trip_4 Lab_4/Trip4.cpp)
add_executable(Lab_4 Lab_4/Lab_4.cpp)
target_link_libraries(Lab_4 Trip_4 ${EXTRA_LISTS})
Your exact intentions are not completely clear, but the above are the simplest possible instructions according to your current question description.
Are you absolutely sure your CMake is version 2.6? You should update the version to whatever CMake you are currently using (type cmake --version to find out).
To add your sources to CMake project, you can use the aux_source_directory command. If your sources are in Lab_4 folder, then you can do the following:
project(Lab_4)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/Lab_4 LAB4_SOURCES)
add_executable(Lab_4 ${LAB4_SOURCES})
The CMAKE_CURRENT_SOURCE_DIR is the path of the directory where the current CMakeFiles.txt is. The example above won't build Trip_4.cpp as a library but use it as another source file together with Lab_4.cpp.
If you wish to build the Trip_4.cpp as library fist I recommend to separate it from Lab_4.cpp to make later uses easier. An example directory structure could be:
MyProject/
|-- app/
| |-- src/
| | `-- Lab_4.cpp
| |-- inc/
| `-- CMakeLists.txt
|-- lib/
| |-- src/
| | `-- Trip_4.cpp
| |-- inc/
| `-- CMakeLists.txt
`-- CMakeLists.txt
In this case the MyProject/lib/CMakeLists.txt will contain something like the following:
project(Trip_4)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src TRIP4_SOURCES)
add_library(Trip_4 ${TRIP4_SOURCES})
The MyProject/app/CMakeLists.txt will be almost the same as I showed in my first example, except now it has to maintain its dependency on Trip_4 library:
project(Lab_4)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/../lib
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src LAB4_SOURCES)
add_executable(Lab_4 ${LAB4_SOURCES})
add_dependencies(Lab_4 Trip_4)
target_link_libraries(Lab_4 Trip_4)
Finally, the MyProject/CMakeLists.txt has to tie together the library and the executable subprojects as follows:
project(MyProject)
add_subdirectory(lib)
add_subdirectory(app)
I have a problem building a CMake project with a static library.
My project strucutre looks like that:
Foo/
|-- CMakeLists.txt
|-- lib/
|-- CMakeLists.txt
|-- libA/
|-- CMakeLists.txt
|-- libA.cpp
|-- libA.h
|-- libAB.h
|-- src/
|-- CMakeLists.txt
|-- main.cpp
|-- srcDirA/
|-- CMakeLists.txt
|-- srcA.h
|-- srcDirB/
|-- CMakeLists.txt
|-- srcB.cpp
|-- srcB.h
And the */CMakeLists.txt look like that:
Foo/CMakeLists.txt:
cmake_minimum_required(VERSION 3.5.1)
project(FOO)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(lib)
add_subdirectory(src)
Foo/lib/CMakeLists.txt:
add_subdirectory(libA)
Foo/lib/libA/CMakeLists.txt:
add_library (staticLibA STATIC libA.cpp)
Foo/src/CMakeLists.txt:
add_subdirectory(srcDirA)
add_subdirectory(srcDirB)
include_directories(".")
add_executable(foo main.cpp)
target_link_libraries(foo LINK_PUBLIC libA)
Foo/src/srcDirA/CMakeLists.txt is empty
Foo/src/srcDirB/CMakeLists.txt is empty
Now I am trying to include the header from my static library into my main project like this:
Foo/src/main.cpp:
#include "srcDirB/srcB.h"
#include "libA/libA.h"
int main() {
//...
return 0;
}
If I try to build this with CMake, libA is generated but I am getting a fatal error:
libA/libA.h: No such file or directory.
Does anyone know what I am doing wrong? Do I have to create a ProjectConfig.cmake file?
You don't need to create any Config files; these are for importing 3rd-party projects.
Since you're using CMake >= 3.5.1, you can esaily specify usage requirements for your libraries. Usage requirements are things like flags or include directories which clients need to build with the library.
So in Foo/lib/libA/CMakeLists.txt:, you'll do this:
add_library (staticLibA STATIC libA.cpp)
target_include_directories(staticLibA INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/..)
Or, if you want the same include directory to apply to libA itself (which is likely), use PUBLIC instead of INTERFACE.
That's all you really need to do. However, given the modern CMake you're using, you should replace your use of the legacy keyword LINK_PUBLIC with its modern equivalent PUBLIC.
Also, since you mention both CMakeLists in .../srcDir* are empty, why have them there in the first place? You can easily get rid of both them and the related add_subdirectory calls.