By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

What do I need to do to use a c library in a c++ program?

P: n/a
Hi,

I have a c++ program, and I would like to use a c library (e.g. curl
library for http).

Where are the things i need to do to use a c library in a c++
program?

I read some articles that i need to wrap 'extern "C"' in my c-code.h:
from http://www.parashift.com/c++-faq-lit...c-and-cpp.html

extern "C" {
#include "my-C-code.h"
}

But my question is I currently is NOT doing that and yet my program
still compiles. So why do I need to put the 'extern "C"' ?

Thank you.

Aug 10 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I have a c++ program, and I would like to use a c library (e.g. curl
library for http).

Where are the things i need to do to use a c library in a c++
program?

I read some articles that i need to wrap 'extern "C"' in my c-code.h:
from http://www.parashift.com/c++-faq-lit...c-and-cpp.html

extern "C" {
#include "my-C-code.h"
}

But my question is I currently is NOT doing that and yet my program
still compiles. So why do I need to put the 'extern "C"' ?
extern "C" defines "c linkage". The C++ compiler by default "mangles"
names of symbols (like functions), so that the C++ function

void foo();

gets not the same name in the object file (and therefore the library) as
if it were compiled in plain C. Wrapping the C-library's functions in
extern "C" tells the C++ compiler to disable name mangling for the
symbols defined as extern "C".

When you do not do this, you should get errors at *link* time, not when
compiling your code.

Cheers,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Aug 10 '07 #2

P: n/a
He************@gmail.com wrote:
Hi,

I have a c++ program, and I would like to use a c library (e.g. curl
library for http).

Where are the things i need to do to use a c library in a c++
program?

I read some articles that i need to wrap 'extern "C"' in my c-code.h:
from http://www.parashift.com/c++-faq-lit...c-and-cpp.html

extern "C" {
#include "my-C-code.h"
}

But my question is I currently is NOT doing that and yet my program
still compiles. So why do I need to put the 'extern "C"' ?
--------------------------------- foo.c
int foo(const char*) { return 42; }
--------------------------------- foo.h
int foo(const char*);
--------------------------------- main.cpp
#include "foo.h"
int main() {
return foo("hello");
}
--------------------------------- Compilation (and linking):
gcc foo.c
g++ main.c foo.obj
---------------------------------
Does this work? No. You can compile both using C++ compiler, of
course, but what if the 'foo' function comes in a pre-made library
for you? The library contains the symbol (something like '_foo')
with no information about the argument type or the return value
type, needed by C++. And the C++ compiler creates a symbol with
the name *mangled* with all the type information. As the result,
the linker cannot match the function needed by 'main' and the one
provided by 'foo.obj'.

What can you do? Try
------------------------------------ main.cpp
extern "C" {
#include "foo.h"
}

int main() {
return foo("hello");
}
-------------------------------------------------
and the rest without change. Does it work now?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 10 '07 #3

P: n/a
He************@gmail.com wrote:
Hi,

I have a c++ program, and I would like to use a c library (e.g. curl
library for http).

Where are the things i need to do to use a c library in a c++
program?

I read some articles that i need to wrap 'extern "C"' in my c-code.h:
from http://www.parashift.com/c++-faq-lit...c-and-cpp.html

extern "C" {
#include "my-C-code.h"
}

But my question is I currently is NOT doing that and yet my program
still compiles. So why do I need to put the 'extern "C"' ?

Thank you.
If the C library header files were written keeping in mind that they may
be used in a C++ program, you may not have to use any extern at all. In
other words, if the 'extern "C"' construct is already there in the
library's header files, you shouldn't be doing that in your own code.
See point no. 19, Make header files compatible with C and C++, here:
http://www.mozilla.org/hacking/portable-cpp.html.

Out of curiosity, what compiler are you using?

regards,
->HS

Aug 10 '07 #4

P: n/a
He************@gmail.com wrote:
I read some articles that i need to wrap 'extern "C"' in my c-code.h:
from http://www.parashift.com/c++-faq-lit...c-and-cpp.html

extern "C" {
#include "my-C-code.h"
}

But my question is I currently is NOT doing that and yet my program
still compiles. So why do I need to put the 'extern "C"' ?
Most C library headers have a extern "C" inside them (inside some
#ifdef CPLUPLUS or whatever) so you don't need to specify it yourself.
That's most probably the case with that library you are using.
Aug 11 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.