当前位置:网站首页>Mixed compilation of C and CC
Mixed compilation of C and CC
2022-07-05 04:04:00 【1mmorta1】
Problem principle
I always thought gcc The compiled file is C Type object file , It has nothing to do with the suffix of the source file . When doing a small experiment this time, I found that the form of the target file is determined by the suffix .
start.h
void start();
start.c
#include <stdio.h>
void start(){
printf("start\n");
}
test.cc
#include <stdio.h>
#include <string.h>
#include "start.h"
int main(){
printf("main\n");
start();
}
* personal_use gcc -c start.c test.cc
* personal_use ld start.o test.o -lc
ld: warning: cannot find entry symbol _start; defaulting to 00000000004002c0
test.o: In function `main': test.cc:(.text+0x11): undefined reference to `start()'
* personal_use nm start.o
U _GLOBAL_OFFSET_TABLE_
U puts
0000000000000000 T start
* personal_use nm test.o
U _GLOBAL_OFFSET_TABLE_
0000000000000000 T main
U puts
U _Z5startv
It can be seen that although they are all used gcc Compilation of , however test.cc The target file for is obviously cpp Formal ( its start Functional symbol It's decorated ), and start.c It is c Form of object file .
So when linking ,test.cc I can't find it _Z5startv This symbol , because test.o There is symbol Name is start .
To sum up , The essence of the problem is actually c and cpp The handling of symbols is different ,cpp Will be embellished with symbols , and c Can't , As a result, the same function in the source code will be different when it becomes a symbol , Cause ambiguity .
resolvent
The simplest thing is to change them into Chengdu c Files or both cpp file , Of course, you can also use both g++ Compile Links ( All become cpp Type object file or c Type object file )
You can put test.cc Modify your file
#include <stdio.h> #include <string.h> #ifdef __cplusplus extern "C" { #endif #include "start.h" #ifdef __cplusplus } #endif int main(){ printf("main\n"); start(); }
In this way test.cc As stated in start The function uses c Type symbol, No ambiguity .
边栏推荐
- [array]566 Reshape the matrix - simple
- Containerization Foundation
- How does the applet solve the rendering layer network layer error?
- Clickhouse materialized view
- Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
- Number of possible stack order types of stack order with length n
- Threejs rendering obj+mtl model source code, 3D factory model
- Why is there a reincarnation of 60 years instead of 120 years in the tiangan dizhi chronology
- 基于TCP的移动端IM即时通讯开发仍然需要心跳保活
- Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘
猜你喜欢
10种寻址方式之间的区别
Wechat applet development process (with mind map)
灵魂三问:什么是接口测试,接口测试怎么玩,接口自动化测试怎么玩?
PlasticSCM 企业版Crack
UI自動化測試從此告別手動下載瀏覽器驅動
Differences among 10 addressing modes
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
在线文本行固定长度填充工具
Why do big companies such as Baidu and Alibaba prefer to spend 25K to recruit fresh students rather than raise wages by 5K to retain old employees?
laravel8 导出Excle文件
随机推荐
Enterprise level: spire Office for . NET:Platinum|7.7. x
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
Clickpaas low code platform
EasyCVR更改录像存储路径,不生成录像文件如何解决?
postman和postman interceptor的安装
How to use jedis of redis
Pyqt5 displays file names and pictures
IronXL for . NET 2022.6
Use of vscode software
[software reverse - basic knowledge] analysis method, assembly instruction architecture
lds链接的 顺序问题
Three level linkage demo of uniapp uview u-picker components
行为感知系统
Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
kubernetes集群之调度系统
反絮凝剂-氨碘肽滴眼液
CTF stegano practice stegano 9
JVM garbage collection
About the recent experience of writing questions
@Transactional 注解导致跨库查询失效的问题