2

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?

stevieb
  • 9,065
  • 3
  • 26
  • 36
  • what does `ldd ver` show? – ysth Jan 15 '17 at 17:49
  • Is it for sure that `#include ` draws the right file? Can you test to add declaration `char * wiringPiVersion(void);` right in front of your function `main`? – Stephan Lechner Jan 15 '17 at 17:51
  • @ysth `ldd (Debian GLIBC 2.19-18+deb8u4) 2.19` – stevieb Jan 15 '17 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 '17 at 17:53
  • I meant running ldd on your test program – ysth Jan 15 '17 at 17:54
  • @ysth the app never gets compiled into an executable – stevieb Jan 15 '17 at 17:56
  • Also, moving the c file into the directory where the modified header is and changing to `#include "wiringPi.h"` doesn't help either – stevieb Jan 15 '17 at 17:57
  • and `-lwiringPi` draws the binary which you compiled *after* you added function `wiringPiVersion()`, for sure? – Stephan Lechner Jan 15 '17 at 18:11
  • well, there's only one on my system, and I verified that it was updated, and to boot, my Perl XS picks it up fine – stevieb Jan 15 '17 at 18:15
  • "actually it's a `#define" what is actually a #define? Please post real unedited code that is known to reproduce the problem. – n. m. could be an AI Jan 15 '17 at 18:19
  • @StephanLechner I'm wrong... if I use `-L /usr/local/lib` where the updated `.so` is, it works! The only other .so is in the wiringPi compilation directory that's far away from where I'm compiling. I wouldn't think that one would be included. So what you mentioned led to the answer (adding the full library path to the compilation command) – stevieb Jan 15 '17 at 18:20
  • @n.m. I apologize. Question updated. – stevieb Jan 15 '17 at 18:21

2 Answers2

3

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.

stevieb
  • 9,065
  • 3
  • 26
  • 36
1

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.

Community
  • 1
  • 1
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58