CMake C/CPP 混合编译

其实主要是要记录一下使用CMake C/CPP混编的一些细节, 要不然会被坑的很惨, 顺便把C/CPP混编原理和实现也说一下.

C/C++混合编程是什么

就像问题本身所说, C/C++混合编程也就是一个工程中, 在C函数中调用C++函数的方法, 在C++的函数中能够调用C函数的方法.

C/C++混合编程有什么用

在我们日常开发中, 也许会遇到这么一些情况, 同事A, C非常牛逼, 但是对C++一窍不通; 同事B, C++信手拈来, 但是对C却满头雾水. 但是在工作中有这么一种需求, 同事A需要用到C++的方法, 同事B需要用到C的方法, 这怎么办?

没错, 最简单的就是, 同事A把C的代码写好, 然后同事B只管调用即可, 同理, 同事A只管调用同事B写好的C++代码, 各司其职, 提高工作效率.

C/C++混合编程应该怎么实现

那么, 这混合编程究竟要怎么实现呢?

在介绍之前, 我们先简单了解下以下几个概念

函数重载(Overloading)

C++和Java中的函数重载的定义一致,

即在相同的作用域内, C++允许多个函数名称相同, 而形参列表不同:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void test() { // 没有参数
}
void test(int a) { // 有一个int类型的形参
}
void test(double a) { // 有一个double类型的形参
}
void test(int a, int b) { // 有两个int类型的形参
}
int test() { // 报错, 函数重载体现在函数名相同形参列表不同和返回类型无关
}

然而大家有没有想过为什么C++支持函数重载, 而C却不支持函数重载呢?

这个就要涉及到C++的名字改编机制了. 请往下看~

C++的名字改编机制

在C中

1
2
3
4
5
void test(); // 该函数编译后编译器会对函数名称改写成`_test`
void run() {
test();
}

output:

1
2
3
4
5
Undefined symbols for architecture x86_64:
"_test", referenced from:
run in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

ps: 不提供test()函数的实现是让CMake链接的时候报错, 这样我们才能看清楚test()函数的真面目!

1
2
3
4
5
void test(int a); // 该函数编译后编译器改写函数名后依然是 _test
void run() {
test(10);
}

output:

1
2
3
4
5
Undefined symbols for architecture x86_64:
"_test", referenced from:
run in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

在C++中

1
2
3
4
5
void test(); // 该函数编译后编译器会对函数名称改写成 test()
void run() {
test();
}

output:

1
2
3
4
5
Undefined symbols for architecture x86_64:
"test()", referenced from:
run() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1
2
3
4
5
void test(int a); // 该函数编译后编译器改写函数名后是 test(int)
void run() {
test(10);
}

output:

1
2
3
4
5
Undefined symbols for architecture x86_64:
"_test", referenced from:
run in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

ps : 有的系统的编译器会编译成_test_int 这种格式, 名字改编机制只是一种思路, 并没有一种唯一的命名规范, 不同的编译器命名规范不同, 但是思路一致! 如下:

1
2
3
4
void test(); // 编译后生成: test() 或 _test
void test(int a); // 编译后生成: test(int) 或 _test_int
void test(double a); // 编译后生成: test(double) 或 _test_double
void test(int a, int b); // 编译后生成: test(int, int) 或 _test_int_int

extern 及 extern “C”

extern相信大家比较熟悉, 它一般用来声明一个函数, 全局变量的作用域. extern告诉编译器, 其声明的函数和变量可以供本文件或者其他文件使用. 这里不再赘述.

extern “C” 中的C是什么意思呢?

这里的C不是指C语言这一门语言, 而是表示一种编译和链接的规约. C表示符合C语言的编译和连接规约的任何语言,如Fortran(公式翻译)、assembler(汇编语言)等。

ps: extern “C” 只是指定编译和链接的规约, 并不会影响语义, 所以在C++文件中该怎么写还得怎么写, 必须遵循C++的语法规范.

在C++源文件的语句前加上 extern “C” 的作用就是告诉编译器, 这一段代码按照类C的编译和链接规约来编译和链接(对, 也就是按照类C的函数命名规范编译)

小结

通过上面几个例子, 相信大家很容易就能知道为什么C++支持重载而C不支持重载了.
因为C++有名字改编机制而C没有!
所以在C中, 只要函数名相同, 不管你的形参列表如何南辕北辙, 编译器均会将其编译为同一函数名, 这样在程序执行过程中就会造成函数调用的二义性(也就是对于相同函数名的函数, 程序并不知道应该调用哪一个函数), 这是不允许的, 所以会报错.
然而对于C++而言, 尽管他们的函数名相同, 但是因为他们的形参列表不同, 编译器编译后实际上会为他们改名为不同名字的函数, 所以程序执行调用函数的时候并不会产生二义性, 因此C++允许函数重载.
这里扯一句题外话, C++的重载被认为不是多态, 因为多态是动态运行时对方法的绑定, 而C++的函数重载最多算是编译时的”多态”. (这句话不一定正确, 请大家纠正)

CPP调用C代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 第一种方法
#include "cpp.h"
extern "C" {
#include "c.h"
}
// 第二种方法
// c.h 文件定义规则
#if defined(__cplusplus)
extern "C" {
#endif
// Do something...
#if defined(__cplusplus)
}
#endif

这里有一点需要注意当CMakeproject(projectname LANGUAGES CXX)方法指定了语言时, CMake只会编译指定的语言的代码, 而导致C语言代码不被编译, 这里需要特别注意

总结

  1. 当需要使用到C/CPP混编时需要用到extern "C"
  2. CMake指定了语言时, 只有指定的语言才会参与编译

参考资料

https://stackoverflow.com/questions/4598308/how-do-i-compile-and-link-c-code-with-compiled-c-code
http://guangming008.blog.163.com/blog/static/1203968201011634426908/
http://www.jianshu.com/p/8d3eb96e142a