473,796 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extern "C" issues

Hi All,

I am a newbie to C++ and I'm trying to figure this out. Can you please
help?

We have 3rd party library that's written in c++. We don't have the
source for it. ldd on that shared library indicates that it depends on
libstdc++. It has a printf() like function called

void xxx_print (char *format, ...)

The executable I built has to source files file_1.cpp and file_2.cpp.

In file_1.cpp, I had this line.

extern void xxx_print (char *format, ...);

In file_2.cpp, I had this line (accidentally)

extern "C" void xxx_print (char *format, ...);

When I ran the executable, the moment xxx_print statement was executed
in any function in file_2.cpp, the process died with exit code 127.
gdb didn't catch anything -- said it's because of GDB's internal
error.

I took off extern "C" (actually, included the header file) and all is
well now. I know that extern "C" is for linking against C code to take
care of name-mangling issues but in this case I had no issues building
and everything is fine as long as no xxx_print is done in file_2.cpp.
All xxx_print statements in file_1.cpp work fine.

I don't know what is messed up. Eventhough the problem is solved I
wanted to understand this better. Your help is greatly appreciated.

Thanks,
B

Feb 21 '07 #1
2 1906
* Bharath:
>
I am a newbie to C++ and I'm trying to figure this out. Can you please
help?

We have 3rd party library that's written in c++. We don't have the
source for it. ldd on that shared library indicates that it depends on
libstdc++. It has a printf() like function called

void xxx_print (char *format, ...)

The executable I built has to source files file_1.cpp and file_2.cpp.

In file_1.cpp, I had this line.

extern void xxx_print (char *format, ...);

In file_2.cpp, I had this line (accidentally)

extern "C" void xxx_print (char *format, ...);

When I ran the executable, the moment xxx_print statement was executed
in any function in file_2.cpp, the process died with exit code 127.
gdb didn't catch anything -- said it's because of GDB's internal
error.

I took off extern "C" (actually, included the header file) and all is
well now. I know that extern "C" is for linking against C code to take
care of name-mangling issues but in this case I had no issues building
and everything is fine as long as no xxx_print is done in file_2.cpp.
All xxx_print statements in file_1.cpp work fine.

I don't know what is messed up. Eventhough the problem is solved I
wanted to understand this better. Your help is greatly appreciated.
It's funny your linker didn't complain.

Perhaps the library provides various overloads of the function name,
i.e. different functions of the same name (it's a C++ library).

Anyway, 'extern "C"' may do more than provide simplified C-compatible
name mangling: it may also influence the machine code level calling
convention, how arguments are passed.

To avoid such problems, if possible use the library's own header files
instead of providing DIY declarations of the library functions used.

Even better advice might be to not use that particular library, because
"..." is unsafe, indicating a low-quality library, and having the format
string passed as 'char*' rather than 'char const*' also indicates a
low-quality library (although that might just be your declaration).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 21 '07 #2
On Feb 20, 7:41 pm, "Alf P. Steinbach" <a...@start.now rote:
* Bharath:


I am a newbie to C++ and I'm trying to figure this out. Can you please
help?
We have 3rd party library that's written in c++. We don't have the
source for it. ldd on that shared library indicates that it depends on
libstdc++. It has a printf() like function called
void xxx_print (char *format, ...)
The executable I built has to source files file_1.cpp and file_2.cpp.
In file_1.cpp, I had this line.
extern void xxx_print (char *format, ...);
In file_2.cpp, I had this line (accidentally)
extern "C" void xxx_print (char *format, ...);
When I ran the executable, the moment xxx_print statement was executed
in any function in file_2.cpp, the process died with exit code 127.
gdb didn't catch anything -- said it's because of GDB's internal
error.
I took off extern "C" (actually, included the header file) and all is
well now. I know that extern "C" is for linking against C code to take
care of name-mangling issues but in this case I had no issues building
and everything is fine as long as no xxx_print is done in file_2.cpp.
All xxx_print statements in file_1.cpp work fine.
I don't know what is messed up. Eventhough the problem is solved I
wanted to understand this better. Your help is greatly appreciated.

It's funny your linker didn't complain.

Perhaps the library provides various overloads of the function name,
i.e. different functions of the same name (it's a C++ library).

Anyway, 'extern "C"' may do more than provide simplified C-compatible
name mangling: it may also influence the machine code level calling
convention, how arguments are passed.

To avoid such problems, if possible use the library's own header files
instead of providing DIY declarations of the library functions used.

Even better advice might be to not use that particular library, because
"..." is unsafe, indicating a low-quality library, and having the format
string passed as 'char*' rather than 'char const*' also indicates a
low-quality library (although that might just be your declaration).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -
Thanks. That helps.

B

Feb 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4260
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I don't need an "extern C" linkage, I just need to define a type to cast a resolved function address.. Any ideas? Basically , I need something like this..
1
2651
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
10
6200
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
12
2730
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external linkage? Not much on this in the FAQ or tutorials.
19
3862
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
4
4759
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a global function with the extern "C" prefix. I was leaning toward using the static method approach until I saw a posting that said compatibility between static C++ functions and C is not guaranteed in the respective language standards. This made me...
4
11450
by: salkesh | last post by:
Hi, I have got a C++ code which has a namespace definition and some functions in it - namespace A { int func1(); int func2();
4
6260
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare functions written in the "SomeLanguage". But I am quite confused what information does the linkage directive tells the compiler.The "generated" function name? The way the arguments are ordered?Or something else? And I am still wondering why extern...
8
1680
by: bob | last post by:
Hi, we're faced with a horrible scenario whereby we are not able to access exported classes in a DLL due to cross compiler issues. Traditional approaches such as using extern "C" and LoadLibrary (nothing to do with C++ I know but I'll get to my question... :). Anyways, we need to access our exported objects and the only safe route I know to get to the destination dll/lib from our client (not in same environment) is by using extern "C"...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.