当前位置:网站首页>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 .
边栏推荐
- 输入的查询SQL语句,是如何执行的?
- The development of mobile IM based on TCP still needs to keep the heartbeat alive
- EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
- [wp][introduction] brush weak type questions
- Behavior perception system
- Threejs factory model 3DMAX model obj+mtl format, source file download
- 灵魂三问:什么是接口测试,接口测试怎么玩,接口自动化测试怎么玩?
- Three level linkage demo of uniapp uview u-picker components
- Excuse me, my request is a condition update, but it is blocked in the buffer. In this case, can I only flush the cache every time?
- ClickPaaS低代码平台
猜你喜欢
Online text line fixed length fill tool
The new project Galaxy token just announced by coinlist is gal
“金九银十”是找工作的最佳时期吗?那倒未必
10种寻址方式之间的区别
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?
NEW:Devart dotConnect ADO.NET
It took two nights to get Wu Enda's machine learning course certificate from Stanford University
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
How about programmers' eyesight| Daily anecdotes
How to realize real-time audio and video chat function
随机推荐
ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
provide/inject
laravel8 导出Excle文件
Web components series (VII) -- life cycle of custom components
MindFusion.Virtual Keyboard for WPF
Behavior perception system
Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
Differences among 10 addressing modes
[untitled]
Deep learning - LSTM Foundation
Redis之Jedis如何使用
Pyqt pyside custom telescopic menu bar sharing (including tutorial)
Installation of postman and postman interceptor
长度为n的入栈顺序的可能出栈顺序
open graph协议
优先使用对象组合,而不是类继承
Clickhouse materialized view
Threejs Internet of things, 3D visualization of factory
Deflocculant aminoiodotide eye drops
Containerd series - what is containerd?