当前位置:网站首页>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 .
边栏推荐
- English essential vocabulary 3400
- Redis source code analysis: redis cluster
- Why can't all browsers on my computer open web pages
- Judge whether the stack order is reasonable according to the stack order
- 3. Package the bottom navigation tabbar
- Is "golden nine and silver ten" the best time to find a job? Not necessarily
- 请问一下我的请求是条件更新,但在buffer中就被拦截了,这种情况我只能每次去flush缓存么?
- CTF stegano practice stegano 9
- Rome链分析
- ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
猜你喜欢
![[数组]566. 重塑矩阵-简单](/img/3c/593156f5bde67bd56828106d7bed3c.png)
[数组]566. 重塑矩阵-简单

DMX parameter exploration of grandma2 onpc 3.1.2.5

10种寻址方式之间的区别

Pyqt pyside custom telescopic menu bar sharing (including tutorial)

Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?

花了2晚,拿到了吴恩达@斯坦福大学的机器学习课程证书

线上故障突突突?如何紧急诊断、排查与恢复

已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘

函数基础学习02

An elegant program for Euclid‘s algorithm
随机推荐
Containerization Foundation
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?
Open graph protocol
Threejs realizes sky box, panoramic scene, ground grass
As soon as I write the code, President Wang talks with me about the pattern all day
ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
What is the reason why the webrtc protocol video cannot be played on the easycvr platform?
我国算力规模排名全球第二:计算正向智算跨越
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
Clickpaas low code platform
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)
[punch in questions] integrated daily 5-question sharing (phase III)
UI自動化測試從此告別手動下載瀏覽器驅動
Rome chain analysis
Uni app common functions /api
陇原战“疫“2021网络安全大赛 Web EasyJaba
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
Assembly - getting started
speed or tempo in classical music