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

H

Herman.Schultz

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-lite/mixing-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.
 
V

Victor Bazarov

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-lite/mixing-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");
}
 
H

H.S.

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-lite/mixing-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
 
D

Daniel Kraft

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-lite/mixing-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
 
J

Juha Nieminen

I read some articles that i need to wrap 'extern "C"' in my c-code.h:
from http://www.parashift.com/c++-faq-lite/mixing-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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top