当前位置: 代码迷 >> 综合 >> cmake的使用
  详细解决方案

cmake的使用

热度:15   发布时间:2024-01-30 06:01:27.0

一个简单的示例:

1.写CMakeLists.txt

cmake_minimum_required(VERSION 2.8.9)    //版本
project (test)                          //项目名称
add_executable(test test.cpp)     //可执行文件名 + 源文件

2.利用cmake生成MakeFile文件

cmake [path]

3.make

make

4.执行文件

./test

 

多文件结构

/* 项目结构* project test*    CMakeLists.txt*    /header*       head1.h*    /source*       source1.cpp*       source2.cpp*/cmake_minimum_required(VERSION 2.8.9)
project(test)//将头文件引入项目中
include_directories(header)
//或者使用add_subdirectory, CMake会自动读取该目录下的CMakeLists.txt文件
//add_subdirectory(header)//加入source文件
set(SOURCES source/sourcd1.cpp source/source2.cpp)
//或者使用通配符
//file(GLOB SOURCES "source/*.cpp")add_executable(test ${SOURCES})

 

动态编译和静态编译

将add_executable分别替换

add_library(test SHARED ${SOURCES})   //动态
add_library(test STATIC ${SOURCES})   //静态