Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

linking a C-lib to a C++ programm

Posted on 2002-05-07
9
Medium Priority
?
247 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 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