mkdir c语言 头文件,cmake 2 指定头文件目录

背景

之前我们用 g++ 编译项目的时候直接通过 -I 选项来指定头文件的目录,当我们用 cmake 的时候应该怎样才通把头文件目录的信息传递到 g++ 呢?

cmake-02.jpg

实验项目

头文件 headers/Person00.h 包含了 Person 类的声明,src/Person00.cpp 文件中对其进行了实现,src/main.cpp 是真正的使用方。

tree .

.

├── CMakeLists.txt

├── headers

│   └── Person00.h

└── src

├── Person00.cpp

└── main.cpp

2 directories, 4 files

headers/Person00.h

#ifndef __Person_H__

#define __Person_H__

class Person

{

private:

std::string m_name;

public:

void say_hello();

Person(std::string name = "");

};

#endif

src/Person00.cpp

#include

#include "Person00.h"

void Person::say_hello()

{

using namespace std;

cout << "hello my name is " << m_name << endl;

}

Person::Person(std::string name)

{

m_name = name;

}

src/main.cpp

#include

#include "Person00.h"

int main()

{

//using namespace std;

Person p("tom");

p.say_hello();

}

直接使用 g++ 编译

对于这样的一个小项目,直接使用 g++ 编译也非常方便。

# 编译{@class=h5 text-secondary mb-4}

g++ -Iheaders src/main.cpp src/Person00.cpp -o nee-headers

# 执行{@class=h5 text-secondary mb-4}

./nee-headers

hello my name is tom

cmake 编译

cmake 文件的全部内容如下。

# 指定 camke 的最小版本{@class=h5 text-secondary mb-4}

cmake_minimum_required(VERSION 3.0)

# 指定项目名{@class=h5 text-secondary mb-4}

project(nee-headers)

# 指定项目中的源文件列表{@class=h5 text-secondary mb-4}

set(files

src/main.cpp

src/Person00.cpp)

# 指定生成的执行程序的名字{@class=h5 text-secondary mb-4}

add_executable(nee-headers ${files})

# 指定头文件的搜索目录为项目目录下的 headers{@class=h5 text-secondary mb-4}

target_include_directories(nee-headers

PRIVATE

${PROJECT_SOURCE_DIR}/headers)

使用 cmake 编译

# 创建一个专门用来编译的目录{@class=h5 text-secondary mb-4}

mkdir build

cd build

# 用 cmake 生成 Makefile{@class=h5 text-secondary mb-4}

cmake ../

-- The C compiler identification is AppleClang 11.0.0.11000033

-- The CXX compiler identification is AppleClang 11.0.0.11000033

-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc

-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

-- Detecting C compile features

-- Detecting C compile features - done

-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++

-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works

-- Detecting CXX compiler ABI info

-- Detecting CXX compiler ABI info - done

-- Detecting CXX compile features

-- Detecting CXX compile features - done

-- Configuring done

-- Generating done

-- Build files have been written to: /Users/jianglexing/cpps/learning002/build

# 编译{@class=h5 text-secondary mb-4}

make

Scanning dependencies of target nee-headers

[ 33%] Building CXX object CMakeFiles/nee-headers.dir/src/main.cpp.o

[ 66%] Building CXX object CMakeFiles/nee-headers.dir/src/Person00.cpp.o

[100%] Linking CXX executable nee-headers

[100%] Built target nee-headers

# 整个过程会生成如下文件{@class=h5 text-secondary mb-4}

ll -h

total 120

-rw-r--r-- 1 jianglexing staff 13K 10 7 22:53 CMakeCache.txt

drwxr-xr-x 15 jianglexing staff 480B 10 7 22:54 CMakeFiles

-rw-r--r-- 1 jianglexing staff 5.7K 10 7 22:53 Makefile

-rw-r--r-- 1 jianglexing staff 1.4K 10 7 22:53 cmake_install.cmake

-rwxr-xr-x 1 jianglexing staff 29K 10 7 22:54 nee-headers

# 执行程序{@class=h5 text-secondary mb-4}

./nee-headers

hello my name is tom

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值