cmake从入门到精通 cmake ..有什么用( 二 )


  • Set Normal Variable
  • set( ... [PARENT_SCOPE])
  • Set Cache Entry
  • set( ... CACHE [FORCE])
  • Set Environment Variable
  • set(ENV{} ...)
PROJECT_SOURCE_DIR
Top level source directory for the current project.
即是工程的顶级目录
【cmake从入门到精通 cmake ..有什么用】 PROJECT_BINARY_DIR
Full path to build directory for project.
即是编译目录,比如如果你创建了build目录(cd build和cmake ..),则路径为:
PROJECT_SOURCE_DIR/build
如果在工程顶级目录直接进行编译(cmake .)则和PROJECT_SOURCE_DIR一致
案例3-单文件+库文件调用
顶层目录内的文件内容
先编译库文件
库文件放在MathFunctions目录 。
Now we will add a library to our project. This library will contain our own implementation for computing the square root of a number. The executable can then use this library instead of the standard square root function provided by the compiler. For this tutorial we will put the library into a subdirectory called MathFunctions. It will have the following one line CMakeLists.txt file:
  1. 添加CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
add_library(MathFunctions mysqrt.cpp)
2.添加头文件MathFunctions.h
#ifndef __MATH_FUNCTION_H__#define __MATH_FUNCTION_H__double mysqrt(double input);#endif
3.添加实现文件
#include #include double mysqrt(double input){ printf("call mysqrtn"); return sqrt(input);}
此时目录文件为:
4.创建build目录并进行编译
mkdir buildcd buildcmake ..make
此时build目录下生成libMathFunctions.a文件,将其拷贝到上一级目录(即是MathFunctions)
cp libMathFunctions.a ../cd ..ls#可以看到当前libMathFunctions目录的内容CMakeLists.txt MathFunctions.cpp MathFunctions.h build libMathFunctions.a
编译main函数所在文件
  1. 顶层目录CMakeLists.txt文件
cmake_minimum_required (VERSION 2.8)project (03-Tutorial)# The version number.set (Tutorial_VERSION_MAJOR 1)set (Tutorial_VERSION_MINOR 0) # configure a header file to pass some of the CMake settings# to the source codeconfigure_file ( "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in" "${PROJECT_BINARY_DIR}/TutorialConfig.h" ) # add the binary tree to the search path for include files# so that we will find TutorialConfig.hinclude_directories("${PROJECT_BINARY_DIR}")include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")add_subdirectory (MathFunctions)# add the executableadd_executable(tutorial 03-Tutorial.cpp)target_link_libraries (tutorial MathFunctions)
2.main函数所在文件
03-Tutorial.cpp
// A simple program that computes the square root of a number#include #include #include #include "TutorialConfig.h"#include "MathFunctions.h"int main (int argc, char *argv[]){ fprintf(stdout,"%s Version %d.%dn", argv[0], Tutorial_VERSION_MAJOR, Tutorial_VERSION_MINOR); double inputValue; if(argc == 2) { inputValue = https://www.goobye.net/atof(argv[1]); } else { inputValue = 4; } double outputValue = mysqrt(inputValue); fprintf(stdout,"The square root of %g is %gn", inputValue, outputValue); return 0;}
3.编译和执行
mkdir buildcd buildcmake ..make
执行文件./tutorial
lqf@ubuntu:/mnt/hgfs/linux/multimedia/src/project/cmake_learn/03-Tutorial/build$ ./tutorial ./tutorial Version 1.0call mysqrtThe square root of 4 is 2
与之相关的我们还要了解工程参数配置,代码规范与命名规则,文件命名与变量命名规则,脚本配置工具autoconf,代码工程组织架构makefile.
这是对于架构工程的一个技术概括 。对技术感兴趣的朋友可以一一了解下 。