我有2个可执行项目,每个项目都依赖于一个解决方案中的2个库(root CMakeLists.txt
)。构建是可以的,因此我有2个库和2个可执行文件。但是当我尝试在Qt创建者中打开root CMakeLists.txt
而不是4个项目(src1, src2, subproject1, subproject2
)&我在根目录中CMakeLists.txt
只有一个名为"Project"
的项目,其中包含我在"Project"
容器中的4个项目,就像包含文件夹一样。
此外,我无法选择项目进行运行(Set project "Project" as Active Project
),因为这只是一个项目。从Qt运行后,MainProject1
运行了。
StackOverflow和google建议使用add_subdirectory
,但这不起作用。在我CMakeLists
的make in和我已用过的几个项目add_subdirectory
。
Qt version: 5.7.0.
Qt Creator version: 4.0.2.
Ubuntu
请帮我解决这个问题。提前谢谢。
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)
mkdir -p build && cd build && cmake .. && make &&
ll src1/MainProject1 && ll src2/MainProject2 && ll subproject1/libsubproject1.a && ll subproject2/libsubproject2.a
-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
一切都好吗
cmake_minimum_required(VERSION 2.8)
add_subdirectory( src1 )
add_subdirectory( src2 )
cmake_minimum_required(VERSION 2.8)
project(subproject1)
set (sources
./hellow.cpp
./hellow.h
)
add_library(subproject1 STATIC ${sources})
是相同的,而是subproject1
:subproject2
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 )
是相同的,而是MainProject1
:MainProject2
打开root CMakeLists.txt&在build文件夹中配置build。
root
Project
CMakeLists.txt
src1
src2
subproject1
subproject2
root
src1
src2
subproject1
subproject2
CMakeLists.txt
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
构建没问题。
MainProject1
已经投放了。无法选择要运行的项目,因为Qt Creator会将根CMakeLists
视为只有一个项目。
答案 0 :(得分:0)
我会按以下方式构建您的项目:
Root CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(MyComplexProject)
add_subdirectory( src1 )
add_subdirectory( src2 )
add_subdirectory( subproject1 )
add_subdirectory( subproject2 )
<强> SRC1 /的CMakeLists.txt 强>
set (sources
./main.cpp
)
add_executable(MainProject1 ${sources})
target_link_libraries (MainProject1 subproject1 subproject2)
<强> subproject1 /的CMakeLists.txt 强>
set (sources
./hellow.cpp
./hellow.h
)
add_library(subproject1 STATIC ${sources})
请等效修改src2 / CMakeLists.txt和subproject2 / CMakeLists.txt。
注意:只有顶级CMakeLists.txt包含项目定义。其他CMakeLists仅包含目标定义。
target_link_libraries默认情况下会向lib目标添加依赖项。所以它不需要明确地完成。
通过将target_link_libraries的依赖项添加到可执行目标,CMake能够找出正确的构建顺序。
编辑:
查看How to load cmake script with more than one project with Qt Creator是否可以帮助您。