Linux cmake高级用法
程序员文章站
2022-07-01 23:25:45
...
目录结构
[email protected]:~/test$ tree
.
├── build
├── CMakeLists.txt
├── include
│ └── hello.h
├── main.cpp
└── src
└── hello.cpp
文件内容
[email protected]:~/test$ cat include/hello.h
#pragma once
void printHello();
[email protected]:~/test$ cat src/hello.cpp
#include <iostream>
#include "hello.h"
using namespace std;
void printHello()
{
cout << "hello" << endl;
}
[email protected]:~/test$ cat main.cpp
#include "hello.h"
int main(int argc, char **argv)
{
printHello();
return 0;
}
[email protected]:~/test$ cat CMakeLists.txt
# 工程名
project(testCMake)
# 包含头文件目录
include_directories("include")
# 生成库 libTest
add_library(libTest src/hello.cpp)
#生成可执行文件test
add_executable(test main.cpp)
# 链接库 libTest
target_link_libraries(test libTest)
编译
[email protected]:~/test$ cd build/
[email protected]:~/test/build$ ls
[email protected]:~/test/build$
[email protected]:~/test/build$ cmake ..
[email protected]:~/test/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
[email protected]:~/test/build$ make
Scanning dependencies of target libTest
[ 50%] Building CXX object CMakeFiles/libTest.dir/src/hello.o
Linking CXX static library liblibTest.a
[ 50%] Built target libTest
Scanning dependencies of target test
[100%] Building CXX object CMakeFiles/test.dir/main.o
Linking CXX executable test
[100%] Built target test
[email protected]:~/test/build$ ./test
hello
[email protected]:~/test/build$
上一篇: 校园商铺o2o实战(SSM版)