473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

using a C library in a C++ program

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.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 5 '07 #1
1 3213
Vasileios Zografos wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 5 '07 #2

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

Similar topics

3
by: rjaw | last post by:
Hi there, using the udb-type2-driver on z/OS DB version 7, we have a problem getting the connection to the database. The small program we use looks like this: ...
17
by: Ralph | last post by:
Hi all, Recently I have tried to create a static lib using MS VC++. The following are some of the excerpt of my codes: ********* MyFirstStaticLib.c ******************* #include...
2
by: Mary | last post by:
Hello, I am having a problem with the cl compiler. I have written a C class (RegConnect.c) which uses Win32 API functions such as RegOpenKey, RegCloseKey etc. Initially when I was trying to...
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
47
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen,...
2
by: xiaotom | last post by:
I want my software to be independant of operation system and databases. That's why I want to use odbc, and don't want to use MFC. Here I have some questions to ask: 1. On unix (like sun...
6
by: robintw | last post by:
Hi all, I'm trying to use some functions which are in an external library file called qhdlc.lib from within my C++ .Net program. I have a header file for this library (qhdlc.h) and have set the...
18
by: Angus | last post by:
Hello We have a lot of C++ code. And we need to now create a library which can be used from C and C++. Given that we have a lot of C++ code using classes how can we 'hide' the fact that it is...
0
MrMancunian
by: MrMancunian | last post by:
How to create a database connection without using wizards Introduction I've seen a lot of questions on the net about getting data from, and saving data to databases. Here's a little insight how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...

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.