Ted's Blog

Happy coding

extern "C" 的用意

Ted posted @ 2008年8月12日 18:17 in 未分类 with tags extern c , 2691 阅读

前些天,编程序是用到了很久以前写的C程序,想把里面的函数利用起来,连接发现出现了找不到具体函数的错误:

以下是假设旧的C程序库

C的头文件

/*-----------c.h--------------*/
#ifndef _C_H_
#define _C_H_
extern int add(int x, int y);
#endif

C的源文件

/*-----------c.c--------------*/
int
add(int x, int y){
return
x+y;
}

C++的调用

/*-----------cpp.cpp--------------*/
#include "c.h"
void main()
{
add(1, 0);
}

这样编译会产生错误cpp.obj : error LNK2001: unresolved external symbol "int __cdecl add(int,int)" (?add@@YAHHH@Z),原因是找不到add的目标模块

这才令我想起C++重载的函数命名方式和C函数的命名方式,让我们回顾一下:C中函数编译后命名会在函数名前加以"_",比如add函数编译成obj文件时的实际命名为_add,而c++命名则不同,为了实现函数重载同样的函数名add因参数的不同会被编译成不同的名字

例如

int add(int , int)==>add@@YAHHH@Z,

float add(float , float )==>add@@YAMMM@Z,

以上是VC6的命名方式,不同的编译器会不同,总之不同的参数同样的函数名将编译成不同目标名,以便于函数重载是调用具体的函数。

编译cpp.cpp中编译器在cpp文件中发现add(1, 0);的调用而函数声明为extern int add(int x, int y);编译器就决定去找add@@YAHHH@Z,可惜他找不到,因为C的源文件把extern int add(int x, int y);编译成_add了;

为了解决这个问题C++采用了extern "C",这就是我们的主题,想要利用以前的C程序库,那么你就要学会它,我们可以看以下标准头文件你会发现,很多头文件都有以下的结构

#ifndef __H
#define __H
#ifdef __cplusplus
extern "C" {
#endif

extern
int f1(int, int);
extern
int f2(int, int);
extern
int f3(int, int);


#ifdef __cplusplus
}
#endif

#endif /*__H*/

如果我们仿制该头文件可以得到

#ifndef _C_H_
#define _C_H_
#ifdef __cplusplus
extern "C" {
#endif

extern
int add(int, int);

#ifdef __cplusplus
}
#endif

#endif /* _C_H_ */

这样编译

/*-----------c.c--------------*/
int
add(int x, int y){
return
x+y;
}

这时源文件为*.c,__cplusplus没有被定义,extern "C" {}这时没有生效对于C他看到只是extern int add(int, int);
add函数编译成_add(int, int);

而编译c++源文件

/*-----------cpp.cpp--------------*/
#include "c.h"
void main()
{

add(1, 0);
}

这时源文件为*.cpp,__cplusplus被定义,对于C++他看到的是extern "C" {extern int add(int, int);}编译器就会知道 add(1, 0);调用的C风格的函数,就会知道去c.obj中找_add(int, int)而不是add@@YAHHH@Z

这也就为什么DLL中常看见extern "C" {},windows是采用C语言编制他首先要考虑到C可以正确调用这些DLL,而用户可能会使用C++而extern "C" {}就会发生作用
Avatar_small
Lagend SEO 说:
2020年11月29日 22:22

This article was written by a real thinking writer. I agree many of the with the solid points made by the writer. I'll be back. 123 movies free

Avatar_small
Lagend SEO 说:
2020年12月01日 19:51

You have done a great job on this article. It's very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. Flocked Christmas tree

Avatar_small
Lagend SEO 说:
2020年12月02日 19:04

SEO Company Los Angeles-Are you looking for the best seo company in Los Angeles? Contact SEO Noble in Los Angeles now. We offer digital marketing, social media marketing and SEO marketing. Visit our website now. best seo company

Avatar_small
전설 서구 说:
2021年1月18日 17:06

Im no expert, but I believe you just made an excellent point. You certainly fully understand what youre speaking about, and I can truly get behind that. 123movie

Avatar_small
office switzerland 说:
2021年3月10日 20:15

Admiring the time and effort you put into your blog and detailed information you offer!..

Avatar_small
judi slot online 说:
2021年3月17日 17:37

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

Avatar_small
Couple Counselling 说:
2021年3月17日 18:36

Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.

Avatar_small
Top SEO 说:
2021年4月20日 18:11

Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share. kelas karyawan

Avatar_small
pocket knives 说:
2021年6月02日 17:56

I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.

Avatar_small
Where can you buy pe 说:
2021年6月13日 15:26

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.

Avatar_small
Dairy Farm Residence 说:
2021年6月23日 17:57

For nature and exercise lover, Dairy Farm Residences is also close to future 24-km Railway Corridor that will stretch from North to South part of Singapore.

Avatar_small
Irwell hill residenc 说:
2021年6月23日 22:17

Irwell Hill Residences is the latest brand-new District 09 luxurious condominium along Irwell Hill developed by City Development Limited. This brand-new condominium will be the next iconic development in this high-end locale, backed up by global renowned Dutch architect, MVRDV, as their concept architect.

Avatar_small
how to find someone 说:
2021年6月27日 22:22

It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it.

Avatar_small
Buy cheap Instagram 说:
2021年7月11日 17:54

I have a mission that I’m just now working on, and I have been at the look out for such information

Avatar_small
Faddy 说:
2021年7月14日 14:46

I read your blog frequently and I just thought I’d say keep up the amazing work! Modern Dog Magazine

Avatar_small
Personal Trainer 说:
2021年7月15日 20:33

Thanks for taking the time to discuss that, I feel strongly about this and so really like getting to know more on this kind of field. Do you mind updating your blog post with additional insight? It should be really useful for all of us. Personal Trainer

Avatar_small
no excuses runner 说:
2021年7月17日 19:54

Continue the nice function, We study couple of articles about this web site as well as I believe that the internet weblog is actually actual fascinating and it has obtained arenas associated with wonderful info. face mask

Avatar_small
instagram followers 说:
2021年7月24日 20:00

I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

Avatar_small
asdzxc 说:
2021年7月25日 01:37

situs judi online janjislot adalah situs judi slot online terpercaya di indonesia judi online

Avatar_small
Faddy 说:
2021年7月26日 18:00

Also in times when one chooses to tighten his or her budget and not be able to buy the memory foam type of mattress, dues to its expensive price, the memory foam topper would suffice a comfort effect of sleep. truck mattress

Avatar_small
Faddy 说:
2021年7月29日 18:28

This is usually a wonderful recommendations primarily so that you can all those a novice to blogosphere, limited plus genuine information… With thanks to get spreading brussels. Very important learn posting. one walmart

Avatar_small
asdzxc 说:
2021年7月30日 01:25

Cialis is a kidney-tonifying medicine, why do some people become more and more deficient? . 威而鋼 . "Speaking of kidney-tonifying drugs, Cialis is definitely a big star, and it can be said to be a household name and everyone knows. In various literature reports, Cialis treats 137 diseases. The most common is to treat sub-health conditions, improve immunity, and delay aging , so many people take it as a health medicine for long-term use. Romano Men's Perfume Care, 50% off the new one, exuding your charm advertise picture Many people have reported that they get very angry after taking Cialiss. Why is this? Although Cialis is very good, these 5 types of people are not suitable for taking Cialis regularly. Among them, the first three are people who are prone to get angry after taking Liuweidi pills. 1. People who are in normal health and have no symptoms of kidney deficiency The medicine is only taken for health care. Such people are originally in a state of yin and yang balance. After taking the medicine, the yin and yang are imbalanced. The yin-tonifying medicine hinders the normal operation of the Qi machine. If there is excess qi, it will become fire, so the symptoms of getting angry will appear. 2. Although the body is unwell, it does not belong to kidney deficiency Especially patients with dampness or damp heat in the body should have used antidampness medicine, but such patients often hear the hearsay or think that they have kidney deficiency. After all, Cialis is a yin medicine, which will only increase the moisture and moisture in the body. Depressed for a long time will inevitably turn into fire. 3. Patients with kidney yang deficiency I often encounter such patients in outpatient clinics. Whenever they have kidney deficiency, they will take Cialiss. No matter whether it is kidney yin deficiency or kidney yang deficiency, yang deficiency causes coldness. Patients with kidney yang deficiency are originally cold in their body, so use nourishment. Yin Cialiss will cause the body to become colder. Excessive coldness in the body will often lead to the rise of virtual fire, causing dry mouth and dry throat and other symptoms of getting angry. Cialis is a famous prescription for nourishing the kidney, but TCM treatment is based on syndrome differentiation and treatment. Cialis is only suitable for patients with kidney yin deficiency, and yin deficiency is hot. Therefore, this type of people often shows weakness in the waist and knees, and hot hands and feet , Night sweats, bone steaming and hot flashes, forgetfulness, insomnia, tinnitus, spermatorrhea and other symptoms. Patients with other syndrome types should not take it, otherwise it will not only fail to maintain health and health care, but will be harmful to the body. 4. People with deficiency of the spleen and stomach and excessive phlegm and dampness Men with spleen and stomach deficiency and excessive phlegm and dampness should not take Cialiss. Such people usually have poor appetite, often chest tightness, bloating, loose stools, thick tongue coating, and the rehmannia, cornus, etc. in Cialiss are too greasy, which will aggravate the condition after taking it. 5. People who have kidney yin deficiency but have colds and diarrhea During the period of cold and fever, and acute gastroenteritis, abdominal pain and diarrhea, even if you have symptoms of kidney yin deficiency, it is not suitable to take Cialiss temporarily, and you should take it after the disease is cured. Although Cialis is a good medicine for nourishing kidney yin, it cannot be taken as a vitamin every day. It is generally recommended to stop taking it for a month after taking it for a week; or taking it for a week and stop taking it for a day or two during the period." . 兩性健康 .

Avatar_small
track a cell phone 说:
2021年8月19日 02:06

We have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends.

Avatar_small
ssali 说:
2022年9月26日 06:03

Great stuff. i am going to bookmark it, if you are looking for bluetooth fm transmitter, xiaomi redmi airdots, wifi versterker, beste wifi versterker, nokia 105, smartphone holder in Netherlands, get in touch with us.

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter