使用Cmake的add_custom_target建立自定义的Target后,必须将这个Target加入all依赖,否则生成的Makefile不会执行这个Target的内容。这样会有个问题,如果需要编译如下目录:
目录下有N个目录,这些目录下或是目录,或是文件,那么递归下去,就会有若干CMakeLists.txt,也就是说,会有若干个add_custom_target(all ...)的存在。进入单独目录编译或许不要紧,但是如果我们在顶层或者中间层执行cmake生成Makefile,就会报重复Target的错误,这个重复的Target就是多次添加的all。错误如下:
add_custom_target cannot create target "all" because
another target with the same name already exists. The existing target is a
custom target created in source directory
See documentation
for policy CMP0002 for more details.
只能在叶子编译而不能在中上层编译,这样显然不行。一个可行的解决方案是,在向all中添加Target前