Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I've added a new function wiringPiVersion() to wiringPi, but after I build and install the shared library, when I attempt to compile a small C program around it, I get:

wpi_ver.c:(.text+0xc): undefined reference to `wiringPiVersion'

However, when I include it in an XS based Perl module, all works well. I don't know enough about C to figure out what's going wrong here, and I've been searching for the better part of two hours trying different things to no avail.

Here's my small C program to test the new function:

#include <stdio.h>
#include <wiringPi.h>

int main (){
    char * ver = wiringPiVersion();
    printf("wiringPi version: %s\n", ver);
    return 0;
}

Compilation that throws the error:

gcc -o ver wpi_ver.c -lwiringPi

The addition to wiringPi's header file:

extern char * wiringPiVersion(void);

The wiringPi's .c file addition:

#define WPI_VERSION "2.36"

char * wiringPiVersion(void){
    return WPI_VERSION;
}

In my Perl module's XS file, I have:

char *
wiringPiVersion()

...and my Perl module's Makefile.PL

LIBS => ['-lwiringPi'],

...and after re-installing the Perl module, I can access the function without any issues in a test script.

I'm hoping this is something simple I'm overlooking which someone may be able to point out. My question is, how do I rectify this?

share|improve this question
    
what does ldd ver show? – ysth Jan 15 at 17:49
    
Is it for sure that #include <wiringPi.h> draws the right file? Can you test to add declaration char * wiringPiVersion(void); right in front of your function main? – Stephan Lechner Jan 15 at 17:51
    
@ysth ldd (Debian GLIBC 2.19-18+deb8u4) 2.19 – stevieb Jan 15 at 17:51
    
@ysth adding the declaration where you stated results in the same error, before and after commenting out the header include line – stevieb Jan 15 at 17:53
    
I meant running ldd on your test program – ysth Jan 15 at 17:54
up vote 3 down vote accepted

So it turned out that there were two .so files generated when I rebuilt wiringPi... one in the wiringPi's build directory way under my home directory, and the other in /usr/local/lib.

After a tip in comments, I added the library path explicitly:

gcc -o ver wpi_ver.c -L/usr/local/lib -lwiringPi

...and it all fell together and works as expected:

$ ./ver
wiringPi version: 2.36

Note: I have sent Gordon the patch in hopes it gets included in the next wiringPi cut.

Update: I received an email back from Gordon and he stated that currently, only the gpio application has the ability to report the version, so he advised that he's going to add something similar to my patch in a future release.

share|improve this answer

Although already solved, I added this answer to show what gave me the hint. Error message "undefined reference" points to a linker error (cf. answer on SO), so its about checking if the correct library is drawn.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.