当前位置:网站首页>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 .
边栏推荐
- A应用唤醒B应该快速方法
- 灵魂三问:什么是接口测试,接口测试怎么玩,接口自动化测试怎么玩?
- EasyCVR更改录像存储路径,不生成录像文件如何解决?
- How about programmers' eyesight| Daily anecdotes
- Why is there a reincarnation of 60 years instead of 120 years in the tiangan dizhi chronology
- Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘
- [software reverse analysis tool] disassembly and decompilation tool
- 小程序中实现文章的关注功能
- How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
- How does the applet solve the rendering layer network layer error?
猜你喜欢
Laravel8 export excel file
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?
Use threejs to create geometry and add materials, lights, shadows, animations, and axes
postman和postman interceptor的安装
快手、抖音、视频号交战内容付费
It took two nights to get Wu Enda's machine learning course certificate from Stanford University
About the recent experience of writing questions
Threejs implements labels and displays labels with custom styles
Redis source code analysis: redis cluster
What is test development? Why do so many companies hire test developers now?
随机推荐
JWT vulnerability recurrence
我就一写代码的,王总整天和我谈格局...
Is "golden nine and silver ten" the best time to find a job? Not necessarily
【看完就懂系列】一文6000字教你从0到1实现接口自动化
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 export excel file
Judge whether the stack order is reasonable according to the stack order
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
JWT漏洞复现
A real day for Beijing programmers!!!!!
C语言课设:影院售票管理系统
How does the applet solve the rendering layer network layer error?
Threejs implements labels and displays labels with custom styles
BDF application - topology sequence
长度为n的入栈顺序的可能出栈顺序
Longyuan war "epidemic" 2021 network security competition web easyjaba
Use of vscode software
如何实现实时音视频聊天功能
Redis source code analysis: redis cluster
Summary of scene design