C++ source code Cmake generates libraries for each architecture

 The general tutorials on the Internet are handwritten C++ source code into CMakeList

For example:

add_executable (main  a.cpp b.cpp a/c.cpp ... )

 This way of writing is too stupid and not suitable for multi-level directories of projects

iterate over all folders

The following code is Cmake code, I was shocked when I first saw it, but the fact is that it can run

Paste the following code into CMakeList.txt, then execute cmake in the source code directory, or create a build folder in the source code folder, CD to build and execute cmake.. This method is called external build, the created file Will be placed in the build.

#启动遍历方法,从根目录开始遍历文件夹以及编译CPP
function(makeAll)
    # 遍历项目根目录和子目录下所有的 .cpp 文件
    makeDir(*)
    # 当前目录的cpp文件编译
    makeCpp(.)
endfunction()

# 遍历文件夹
function(makeDir dir)
    # 判断字符串是否包含 cmake-build-debug ,将结果输出到indexOfStr变量,若包含返回 > -1的值,若不包含返回-1
    STRING(FIND ${dir} cmake-build-debug indexOfStr)
    if(NOT indexOfStr MATCHES -1)
        # 和 cpp的 return; 一样用法
        return()
    endif()
    file (GLOB dirs ${dir}) # 创建变量files
    foreach(item ${dirs})
        if(${item} EQUAL cmake-build-debug)
            # 和 cpp的 continue; 一样用法
            continue()
        endif()
        if(IS_DIRECTORY ${item}) # 判断是否目录,必须加上${} ,如果不加 ${} 就是字符串,无法判断是否目录
            makeDir(${item}/*) # 递归
            makeCpp(${item}) # 编译cpp
        endif()
        #    message(STATUS ${item})
    endforeach()
endfunction()

# 编译单个目录下的所有cpp文件
function(makeCpp dir)
    file (GLOB cpp_files ${dir}/*.cpp) # 创建变量files
    foreach(item ${cpp_files})
        message("make cpp file ->" ${item})
        string(REGEX REPLACE ".+/(.+)/(.+)\\..*" "\\1-\\2" exe ${item})
        add_executable (${exe} ${item} )
    endforeach()
endfunction()

# 启动
makeAll()

 The red box is to execute cmake and compile the source code after execution

Did not compile successfully! ! ! ! There is an error

 Look at the prompt: fatal error: 'google/protobuf/stubs/common.h' file not found

The reason for the error is that we did not compile the dependent code first, resulting in an error when the file could not be found.

How to solve it, just modify the compilation order

# 定义函数,函数名为 makeAll
function(makeAll)
    # 遍历项目根目录和子目录下所有的 .cpp 文件
    # makeDir(*) 这是老的代码,注释掉

    # 由于gtest被google,因此先编译gtest
    makeDir("/Users/hailong/Documents/CMakeProject/src/gtest")

    #goolgle被下面的4个引用,第二个编译
    makeDir("/Users/hailong/Documents/CMakeProject/src/google")

    #core被后面3个引用,第三个编译
	makeDir("/Users/hailong/Documents/CMakeProject/src/core")

    #后面的三个没有互相引用,编译顺序随意
	makeDir("/Users/hailong/Documents/CMakeProject/src/gamed")
	makeDir("/Users/hailong/Documents/CMakeProject/src/cmn")
	makeDir("/Users/hailong/Documents/CMakeProject/src/events")

endfunction()

After modifying the CMakeList.txt code, execute cmake again. .

 There is no error to prove success, look at the build folder

Guess you like

Origin blog.csdn.net/liuyongjie1992/article/details/132060879