是否有等效的东西qmake -project
会自动从源文件和头文件目录创建一个 CMake 项目?
理想情况下,这应该递归地工作。
不,但这是一个易于设置的项目:
project(myProject)
enable_language(CXX)
file(GLOB SRC_FILES *.cpp)
include_directories(${PROJECT_SOURCE_DIR})
add_executable(myExe ${SRC_FILES})
假设您正在制作可执行文件。add_library
如果您正在制作图书馆,应该使用。如果您的项目位于子目录(如src
和include
.
(我知道这是很久以前问过的,但无论如何我都会发布我的答案,以供将来参考。)
我认为更好的主意不是CMakeLists.txt
每次运行时都自动添加带有全局的所有源,而是使用静态源。我猜最初的意思是一个脚本,它扫描当前目录(递归)以查找源文件并将它们添加到 CMake 文件中。这可能会节省大量将每个源文件的名称复制到 CMake 文件的时间。因此:让我们自动化吧!
创建一个包含cmake-project.sh
以下内容的文件:
#!/bin/bash
# use the first argument as project name
PROJECT_NAME=$1
# find source files, but exclude the build directory
PROJECT_SOURCES=$(find . -iname "*.cpp" -not -path "./build/*")
# find header files, but exclude the build directory;
# only print the name of the directory; only print unique names
PROJECT_SOURCE_DIR=$(find . -iname "*.h" -not -path "./build/*" \
-printf "%h\n" | sort -u)
# The standard content of the CMakeLists.txt can be edited here
cat << EOF > CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
set(PROJ_NAME $PROJECT_NAME )
set(PROJ_SOURCES $PROJECT_SOURCES )
project(\${PROJ_NAME})
include_directories(${PROJECT_SOURCE_DIR})
add_executable(\${PROJ_NAME} \${PROJ_SOURCES})
EOF
然后,使用 . 使文件可执行chmod +x cmake-project.sh
。现在你可以./cmake-project.sh [your_project_name]
在根目录下运行CMakeLists.txt
自动创建一个静态(即没有glob)。
当然,如有必要,您必须进行调整(例如,使用.cc
代替.cpp
),但您明白了。