Solved

linking a C-lib to a C++ programm

Posted on 2002-05-07
9
240 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
ID: 6995132
could you please post the linker command
0
 

Author Comment

by:zulliger
ID: 6995146
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
ID: 6995190
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:zulliger
ID: 6995834
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6996071
.. 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
ID: 6996092
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
ID: 6996161
> .. 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
ID: 6996530
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
ID: 6996531
(no comment :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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