我cmake
用来构建我的项目。对于 UNIX,我想make
从我的项目的根目录键入,并cmake
调用以创建正确的 Makefile(如果它们尚不存在),然后构建我的项目。我希望隐藏cmake
“内部”文件(目标文件、cmake
内部 Makefile 等)(例如放在.build
目录中),这样它就不会弄乱我的项目目录。
我的项目有几个子项目(特别是一个库、一个用户可执行文件和一个单元测试可执行文件)。我希望make
每个子项目的 Makefiles(即我键入并且发生这种情况)执行cmake
(如上所述)并仅构建该子项目(具有依赖关系,因此如果需要,该库将从可执行文件的 Makefiles 构建)。生成的二进制文件(.so 库或可执行文件)应位于子项目的目录中。
我制作了一个 Makefile,它在主要项目上做得有点好,虽然感觉有点 hackish。我无法使用它构建特定的目标,因为我的 Makefile 只是make
在cmake
的构建目录中调用。
请注意,因为该库是唯一的依赖项(并且可能不需要手动构建,并且因为我很懒),所以我在 Makefile 中省略了它。
BUILD_DIR := .build
.PHONY: all clean project-gui ${BUILD_DIR}/Makefile
all: project-gui project-test
clean:
@([ -d ${BUILD_DIR} ] && make -C ${BUILD_DIR} clean && rm -r ${BUILD_DIR}) || echo Nothing to clean
project-gui: ${BUILD_DIR}/Makefile
@make -C ${BUILD_DIR} project-gui
@cp ${BUILD_DIR}/project-gui/project-gui $@
project-test: ${BUILD_DIR}/Makefile
@make -C ${BUILD_DIR} project-test
@cp ${BUILD_DIR}/project-test/project-test $@
${BUILD_DIR}/Makefile:
@[ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR}
@[ -f ${BUILD_DIR}/Makefile ] || (cd ${BUILD_DIR} && cmake ${CMAKE_OPTS} ..)
如果有帮助,这是我的项目结构(如果这是“错误的”,请告诉我——我还在学习cmake
):
project/
project/CMakeLists.txt
project/common.cmake
project/Makefile -- see Makefile above for this; should be replaced with something better, building libproject, project-gui, and project-test
project/libproject/
project/libproject/CMakeLists.txt
project/libproject/libproject.so -- after build
project/libproject/Makefile -- doesn't exist yet; should build libproject only
project/libproject/source/
project/libproject/include/
project/project-gui/
project/project-gui/CMakeLists.txt
project/project-gui/Makefile -- doesn't exist yet; should build libproject then project-gui
project/project-gui/source/
project/project-gui/include/
project/project-test/
project/project-test/CMakeLists.txt
project/project-test/Makefile -- doesn't exist yet; should build libproject then project-test
project/project-test/source/
project/project-test/include/
如果您还没有理解,我基本上是在寻找一种方法来构建项目和子项目,就好像cmake
不存在一样:好像我的项目只包含 Makefile。这可以做到吗?解决方案是优雅的还是混乱的?我应该尝试做其他事情吗?
谢谢!