Solved

linking a C-lib to a C++ programm

Posted on 2002-05-07
9
241 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

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

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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