How to configure CMakeLists.txt files of projects for have separated projects in Qt Creator instead one project
-
wrote on 1 Jun 2017, 12:29 last edited by
I have 2 executable projects each of which depends from 2 libraries in one solution (root
CMakeLists.txt
). Build is ok and as result I have 2 libraries and 2 executable. But when I try to open rootCMakeLists.txt
in Qt creator instead 4 projects (src1, src2, subproject1, subproject2
) &CMakeLists.txt
in the root as result I have only 1 project with name"Project"
which includes my 4 projects in"Project"
container like this is include folders.Also I cannot select project for run (
Set project "Project" as Active Project
) since this is only one project. After run from Qt theMainProject1
was run.StackOverflow and google propoused to use
add_subdirectory
for this but this does not work. In myCMakeLists
's for make depends and several projects I already had usedadd_subdirectory
.Qt version: 5.7.0. Qt Creator version: 4.0.2. Ubuntu
Please help me for fix this. Thanks in advance.
Project structure & manual build & results: Succeed
Project structure:
root src1 (executable which depends from subproject1 and subproject2) CMakeLists.txt main.cpp src2 (executable which depends from subproject1 and subproject2) CMakeLists.txt main.cpp subproject1 (library) CMakeLists.txt hellow.cpp hellow.h subproject2 (library) CMakeLists.txt hellow.cpp hellow.h CMakeLists.txt (the root CMakeLists)
Build & show results:
mkdir -p build && cd build && cmake .. && make && ll src1/MainProject1 && ll src2/MainProject2 && ll subproject1/libsubproject1.a && ll subproject2/libsubproject2.a
Results:
-rwxrwxr-x 1 qqq qqq 13570 Jun 1 10:45 src1/MainProject1* -rwxrwxr-x 1 qqq qqq 13570 Jun 1 10:45 src2/MainProject2* -rw-rw-r-- 1 qqq qqq 2820 Jun 1 10:45 subproject1/libsubproject1.a -rw-rw-r-- 1 qqq qqq 2828 Jun 1 10:45 subproject2/libsubproject2.a
All is ok
CMakeLists.txt
Root CMakeLists.txt
cmake_minimum_required(VERSION 2.8) add_subdirectory( src1 ) add_subdirectory( src2 )
subproject1/CMakeLists.txt
cmake_minimum_required(VERSION 2.8) project(subproject1) set (sources ./hellow.cpp ./hellow.h ) add_library(subproject1 STATIC ${sources})
subproject2/CMakeLists.txt
Is the same but instead
subproject1
:subproject2
src1/CMakeLists.txt
cmake_minimum_required(VERSION 2.8) project(MainProject1) set (sources ./main.cpp ) if (NOT TARGET subproject1) add_subdirectory( ./../subproject1 "${CMAKE_CURRENT_BINARY_DIR}/../subproject1" ) add_subdirectory( ./../subproject2 "${CMAKE_CURRENT_BINARY_DIR}/../subproject2" ) endif() add_executable(MainProject1 ${sources}) target_link_libraries (MainProject1 subproject1) target_link_libraries (MainProject1 subproject2) add_dependencies( MainProject1 subproject1 ) add_dependencies( MainProject1 subproject2 )
src2/CMakeLists.txt
Is the same but instead
MainProject1
:MainProject2
Open in Qt Creator: Failed
Open root CMakeLists.txt & configure for build in build folder.
Result of project structure (only 1 project):
root Project CMakeLists.txt src1 src2 subproject1 subproject2
Expect (4 projects):
root src1 src2 subproject1 subproject2 CMakeLists.txt
Result of build:
cd build && ll src1/MainProject1 && ll src2/MainProject2 && ll subproject1/libsubproject1.a && ll subproject2/libsubproject2.a -rwxrwxr-x 1 qqq qqq 38670 Jun 1 11:10 src1/MainProject1* -rwxrwxr-x 1 qqq qqq 38670 Jun 1 11:10 src2/MainProject2* -rw-rw-r-- 1 qqq qqq 21900 Jun 1 11:10 subproject1/libsubproject1.a -rw-rw-r-- 1 qqq qqq 21908 Jun 1 11:10 subproject2/libsubproject2.a
Build is ok.
Run project
MainProject1
was run. Cannot select project for run since Qt Creator perceives rootCMakeLists
as if it's only one project.Note
-
Hi and welcome to devnet,
First thing you should do is to update Qt Creator. 4.3 just got released with a revamped and improved support for cmake.
-
wrote on 7 Jun 2017, 08:33 last edited by Alexander Symonenko 6 Jul 2017, 08:34
Thank you. After discovering I see that Qt Creator cannot have several projects using cmake like in Visual Studio solutions ;-( It can have only one root project from root CMakeLists.txt. But Qt Creator allow to select which executiable should be run after build of root project:
You should add multiple projects to the root cmake file with help of "add_subdirectory()". Then in Qt Creator you should open the root cmake file. After that you could choose which project to run in the select a kit for running (3) or debugging (4) application pane (check the link).
It sad that you can't (or I don't know how) build only one project. You have to build all projects are added to the root cmake file and then choose which one of them you want to run.
I use Qt Creator 4.0.
https://stackoverflow.com/questions/19466141/how-to-load-cmake-script-with-more-than-one-project-with-qt-creatorAs result of restrict support of multiple projects you cannot select which subproject to compile but can select executiable for run.
-
Thanks for the feedback, you should take a look at the bug report system to see if there's something related and maybe consider a feature request about that matter if nothing already exists.
1/4