当前位置:网站首页>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 .
边栏推荐
- Judge whether the stack order is reasonable according to the stack order
- 特殊版:SpreadJS v15.1 VS SpreadJS v15.0
- It took two nights to get Wu Enda's machine learning course certificate from Stanford University
- Online text line fixed length fill tool
- Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route
- provide/inject
- Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
- 长度为n的入栈顺序的可能出栈顺序种数
- The new project Galaxy token just announced by coinlist is gal
- NEW:Devart dotConnect ADO. NET
猜你喜欢

为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?

Is "golden nine and silver ten" the best time to find a job? Not necessarily

Clickhouse materialized view

@The problem of cross database query invalidation caused by transactional annotation

Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes

企业级:Spire.Office for .NET:Platinum|7.7.x

Threejs realizes sky box, panoramic scene, ground grass

ActiveReportsJS 3.1 VS ActiveReportsJS 3.0

Use of vscode software

How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
随机推荐
Why is there a reincarnation of 60 years instead of 120 years in the tiangan dizhi chronology
It took two nights to get Wu Enda's machine learning course certificate from Stanford University
Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes
Get to know MySQL connection query for the first time
open graph协议
Threejs realizes sky box, panoramic scene, ground grass
Threejs Internet of things, 3D visualization of factory
JVM garbage collection
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
Number of possible stack order types of stack order with length n
ClickPaaS低代码平台
线上故障突突突?如何紧急诊断、排查与恢复
ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
Containerd series - what is containerd?
Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
在线SQL转Excel(xls/xlsx)工具
Laravel8 export excel file
花了2晚,拿到了吴恩达@斯坦福大学的机器学习课程证书
[wp][introduction] brush weak type questions
Interview byte, pass the exam and directly work on three sides. As a result, I found an architect to hang me?