Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Linking a library to a c++ project apparently fails [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Logge
Tux's lil' helper
Tux's lil' helper


Joined: 09 Jul 2004
Posts: 131
Location: Sweden

PostPosted: Fri Apr 11, 2008 12:17 am    Post subject: Linking a library to a c++ project apparently fails [SOLVED] Reply with quote

Hi folks!!!

The last couple of weeks I've been writing a program to be used to visualize a timer and a highscores list for a waterslope.
The graphical UI has not been any kind of problem but now there is with the library I want to use to access and controll a
Velleman k8055 USB-powered i/o expander card. There is an unofficial ebuild for this library among the bugs which I've
successfully installed.

The authors of the library has a site (http://libk8055.sourceforge.net) where it says when explaining how to compile the
program shipped with the package:
Quote:
Own program using the k8055 library
gcc -Wall -lusb -lk8055 main.c
This assumes you have all code in main.c, otherwise add necessary link options


Now to my problem. I've tried compiling the program when using one of the functions in the library but all i get is an undefined
reference error for that function. I've successfully managed to add the library for k8055 and also for libusb using cmake with the
following code but it has probably something to do with the "necessary link options" that I have no idea of who they are
Code:
... snip ...

FIND_LIBRARY( LIBUSB NAMES usb )
FIND_LIBRARY( LIBK8055 NAMES k8055 )
... snip ...
TARGET_LINK_LIBRARIES( my_program ${QT_LIBRARIES} ${LIBUSB} ${LIBK8055})


No complains there but after linking, the following appears
Code:
Linking CXX executable my_program
CMakeFiles/my_program.dir/src/sensorhandler.o: In function `SensorHandler::openDevice()':
sensorhandler.cpp:(.text+0x1f5): undefined reference to `OpenDevice(long)'
collect2: ld returned 1 exit status
make[2]: *** [my_program] Error 1
make[1]: *** [CMakeFiles/my_program.dir/all] Error 2
make: *** [all] Error 2


Anyone of You having any brilliant ideas how to get this to compile as it should??

As You might have noticed, My C++ skills are not that great...

Regards
Oskar
_________________
I never stop to amaze about the advanced features of the human body's simplicity


Last edited by Logge on Fri Apr 11, 2008 10:10 pm; edited 1 time in total
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 14617

PostPosted: Fri Apr 11, 2008 2:56 am    Post subject: Reply with quote

The header file for the library is wrong. I see two problems immediately. First, it does not have an include guard. Second, it assumes that it will only be included in files with C linkage. Since the library is written in C, all symbols provided by it have C linkage. However, your code is written in C++ and defaults to C++ linkage. The library header should have explicitly selected C linkage, so that your program would use C linkage for library functions and C++ linkage for everything else.

Add this block of code at the start of the header file:
Code:
#ifdef __cplusplus
extern "C" {
#endif


and this block at the end of the header file:
Code:
#ifdef __cplusplus
}
#endif


I suggest you submit this to the original maintainers as a patch, so that others benefit from the change.
Back to top
View user's profile Send private message
Logge
Tux's lil' helper
Tux's lil' helper


Joined: 09 Jul 2004
Posts: 131
Location: Sweden

PostPosted: Fri Apr 11, 2008 7:31 am    Post subject: Reply with quote

Huge thanks for those lines!!

After applying those lines to the header my program compiled just fine.
Now I just need to get the hardware to see if the program acctually is going
to work the way as I've been planning.

Huge Thanks!!

regards
Oskar
_________________
I never stop to amaze about the advanced features of the human body's simplicity
Back to top
View user's profile Send private message
Logge
Tux's lil' helper
Tux's lil' helper


Joined: 09 Jul 2004
Posts: 131
Location: Sweden

PostPosted: Fri Apr 11, 2008 8:02 am    Post subject: Reply with quote

I've just submitted the suggested additions to the author of the library.
Also adding a note about it in the bug for libk8055 among Gentoo bugs.
_________________
I never stop to amaze about the advanced features of the human body's simplicity
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum