?
Solved

linking a C-lib to a C++ programm

Posted on 2002-05-07
9
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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