using a C library in a C++ program

  • Thread starter Vasileios Zografos
  • Start date
V

Vasileios Zografos

Hello,

I have a C++ program that calls a function from a C library (its the
Triangle program (http://www.cs.cmu.edu/~quake/triangle.html). The
library is passed as an object file: triangle.o


I compiled the library using the GNU C compiler (gcc) and it compiles fine.

I know compile and link my program using the GNU C++ compiler (g++) like
this:

g++ -Wall -O2 -o main main.cpp triangle.o


The function that I use from triangle.o is "triangulate". However, this
is where the linking fails (the compilation part works fine). The
linking error I get is the following:


In function `Triangulate(Matrix&, Matrix&, Matrix&)':
main.cpp:(.text+0x438): undefined reference to `triangulate(char*,
triangulateio*, triangulateio*, triangulateio*)'
collect2: ld returned 1 exit status


Now, I suspect it has to do something with the fact that my program is
in C++ and triangle in C, but surely this shouldn't be a problem right?
It works perfectly on Windows (VC++) and compiles and links fine (the
exact same program).


Has anyone got any information as to why this might be happening?
Any help would be greatly appreciated.

Thank you
V.Z.
 
V

Victor Bazarov

Vasileios said:
I have a C++ program that calls a function from a C library (its the
Triangle program (http://www.cs.cmu.edu/~quake/triangle.html). The
library is passed as an object file: triangle.o


I compiled the library using the GNU C compiler (gcc) and it compiles
fine.
I know compile and link my program using the GNU C++ compiler (g++)
like this:

g++ -Wall -O2 -o main main.cpp triangle.o


The function that I use from triangle.o is "triangulate". However,
this is where the linking fails (the compilation part works fine). The
linking error I get is the following:


In function `Triangulate(Matrix&, Matrix&, Matrix&)':
main.cpp:(.text+0x438): undefined reference to `triangulate(char*,
triangulateio*, triangulateio*, triangulateio*)'
collect2: ld returned 1 exit status


Now, I suspect it has to do something with the fact that my program is
in C++ and triangle in C, but surely this shouldn't be a problem
right? It works perfectly on Windows (VC++) and compiles and links fine
(the
exact same program).


Has anyone got any information as to why this might be happening?

You need to declare your function 'extern "C"' when compiling your
C++ module ('main.cpp'). If there is a header file which you include
into your 'main.cpp', make sure the declaration of 'triangulate' has
'extern "C"' included _if_ the compilation is with C++ compiler:

#ifdef __cplusplus
extern "C"
#endif
whatever triangulate(...);

If you're not allowed to change the header, try surrounding the include
directive with 'extern "C"':

extern "C"
{
#include "triangle.h"
}

If that doesn't work, you could try to rename and recompile all the
code as C++.

V
 

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,995
Messages
2,570,236
Members
46,823
Latest member
Nadia88

Latest Threads

Top