Solved

linking a C-lib to a C++ programm

Posted on 2002-05-07
9
238 Views
Last Modified: 2010-04-22
i'm programming a small QT-app which should use a C-library (a .a-file) the problem is: all functions which are located in this lib can not be accessed.

--> i'm able to compile my QT-app (containing calls for functions from the C-lib) but i'm not able to link this app, because of the errors (undefined reference...)

i've read to make a
extern "C" { ...... }
around the lib-functions, but then i get other errors...

any ideas?
0
Comment
Question by:zulliger
  • 5
  • 4
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
could you please post the linker command
0
 

Author Comment

by:zulliger
Comment Utility
g++ -O2 -O0 -g3 -Wall -I/usr/src/linux -g -O2 -D__KERNEL__ -DMODULE -D_LOOSE_KERNEL_NAMES -fno-exceptions -fno-check-new -o canfestivalgui canf
estivalgui.o main.o maingui.o  -L/usr/X11R6/lib -L/usr/lib/qt2/lib /usr/lib/libCanOpenMatic.a -lqt -lpng -lz -lm /usr/lib/libjpeg.so -lXext -lX
11 -lresolv -lSM -lICE -lresolv


canfestivalgui.o: In function `CANFestivalGui::sendSDO(void)':
/home/raphael/developing/canfestivalgui/canfestivalgui/canfestivalgui.cpp:83: undefined reference to `Send_SDO(int, struct_s_SDO, int)'


--> i've copied the lib to /usr/lib
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
please post result of (assuming that it is the static lib in question):

nm -gop /usr/lib/libCanOpenMatic.a|egrep -i 'sendSDO|Send_SDO'

dumme Frage: did you verify casesensitiv spelling?
0
 

Author Comment

by:zulliger
Comment Utility
sodummistdiefragegarnicht: yes i did veify casesensitive... :-)

[raphael@sensor raphael]$ nm -gop /usr/lib/libCanOpenMatic.a|egrep -i 'sendSDO|Send_SDO'
/usr/lib/libCanOpenMatic.a:CanOpenMatic.o:000000d0 T Send_SDO
[raphael@sensor raphael]$

i think the problem is related to c/c++ mixing...
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
.. and you have something like
extern "C" {void Send_SDO(int, struct_s_SDO, int); }
in your c++ file?
Then it should work. If it still fails look at the linker map, using the linker's -M option, or -Wl,M for g++.
0
 

Author Comment

by:zulliger
Comment Utility
the problem is:

the CanOpenMatic.c is compiled with gcc. when i insert extern "C" commands, gcc doesn't know them. when i rename the file to CanOpenMatic.cpp and compile it with g++ then it recognizes the extern "C" command, but has problems with some code in this file.
there are lines like:
static int NMTable[MAX_CAN_BUS_ID][NMT_MAX_NODE_ID] = {[0 ... MAX_CAN_BUS_ID-1] = {[0 ... NMT_MAX_NODE_ID-1] = -1}};

and it doesn't know the ...

in other words: i can't make extern "C" around the function prototypes...

do you know what the ... means exactly and how to program their function in C++?

0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
Comment Utility
> .. i can't make extern "C" around the function prototypes...

NO, NO.
You need to make the extern "C" in your c++ files !!
(otherwise it would never be possible to call C funtions from within C++)

> do you know what the ... means exactly
which ... do you mean, that from the error message?
I'd simply forget it ;-)
0
 

Author Comment

by:zulliger
Comment Utility
ok! i've solved the problems... i had to add

#ifdef __cplusplus
extern "C" {
#endif
...
...

to the header-files (which were included once by the c-file and once by the cpp-file) and now it works...

thanks anyway. because i wanna give you the points, i will just accept the last comment.

thanks!
0
 

Author Comment

by:zulliger
Comment Utility
(no comment :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now