pt254
Posts: 2
Joined: Thu May 17, 2012 1:06 pm

wiringPi undefined Reference

Tue Jun 05, 2012 8:16 pm

Hallo,
I want to use the gpio pins and the wiring Pi lib looked good to me (thanks for writing it, if the programmer sees this here).
But when I try to compile my programm I get the errors:
undefined reference to 'wiringPiSetup()'
undefined reference to 'pinMode(int,int)'
undefined reference to 'digitalRead(int)'
collect2: ld returned 1 exit status

I use: g++ pins.cpp -I/usr/local/include -L/usr/local/lib -lwiringPi

my code:

Code: Select all

#include <wiringPi.h>
#include <iostream>
using namespace std;

int main()
{
    if (wiringPiSetup()==-1)
    {
    return 1;
    }
    pinMode(0,INPUT);
    bool wasPressed=false;
    int a;
    while (true)
    {
    a=digitalRead(0);
    if (a==0 && wasPressed) wasPressed=false;
    if (a==1 && !wasPressed)
    {
    cout<<"pressed"<<endl;
    wasPressed=true;
    }
    }
    
}
i copied the command from the wiring pi page (modified it a little as i want to use c++)

what am I doing wrong?

higwoth1
Posts: 26
Joined: Sat May 26, 2012 1:27 am

Re: wiringPi undefined Reference

Wed Jun 06, 2012 10:15 am

If you use gcc instead of g++ it will compile, yes?

I know that doesn't fix your problem as you want to use c++, but it might be a clue as to what is going wrong, for you or somebody else to figure out.

tufty
Posts: 1454
Joined: Sun Sep 11, 2011 2:32 pm

Re: wiringPi undefined Reference

Wed Jun 06, 2012 10:33 am

If it works with gcc and not g++, it's probably wiringPi.h that's missing the habitual

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}
#endif
C++ wrapper for C functions. Without this, the compiler mangles the names into something C++ like, and then (unsurprisingly) can't find them in the linked library.

Simon

pt254
Posts: 2
Joined: Thu May 17, 2012 1:06 pm

Re: wiringPi undefined Reference

Wed Jun 06, 2012 4:06 pm

thank you, there was no wrapper in the lib. after adding that i works.

Ced
Posts: 22
Joined: Thu Jan 03, 2013 5:33 pm
Location: Martinique, FWI

Re: wiringPi undefined Reference

Fri Jan 04, 2013 10:33 pm

pt254 wrote:thank you, there was no wrapper in the lib. after adding that i works.
What did you change to it work ?

IanH2
Posts: 79
Joined: Tue Dec 18, 2012 10:17 am

Re: wiringPi undefined Reference

Sat Jan 05, 2013 8:06 am

Ced wrote:
pt254 wrote:thank you, there was no wrapper in the lib. after adding that i works.
What did you change to it work ?
Something like this will work:

Code: Select all

extern "C" {
#include <wiringPi.h>
}
To see a symbol which has been compiled with a C compiler (the wiringPi functions) from within a C++ program (this one) you need to use extern "C" when declaring it. Fortunately C++ is fairly forgiving about where you do this, so the idiom above works perfectly well.

Some C libraries have 'C++ friendly' header files which do this automatically, but it looks like wiringPi isn't one of them (yet).
-----
https://github.com/IanHarvey

Ced
Posts: 22
Joined: Thu Jan 03, 2013 5:33 pm
Location: Martinique, FWI

Re: wiringPi undefined Reference

Sat Jan 05, 2013 3:32 pm

Something like this will work:

Code: Select all

extern "C" {
#include <wiringPi.h>
}
To see a symbol which has been compiled with a C compiler (the wiringPi functions) from within a C++ program (this one) you need to use extern "C" when declaring it. Fortunately C++ is fairly forgiving about where you do this, so the idiom above works perfectly well.

Some C libraries have 'C++ friendly' header files which do this automatically, but it looks like wiringPi isn't one of them (yet).
where you added these lignes ?

User avatar
gordon@drogon.net
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: wiringPi undefined Reference

Sat Jan 05, 2013 4:01 pm

pt254 wrote:thank you, there was no wrapper in the lib. after adding that i works.
wiringPi has had those wrappers for a very long time now. (6+ months) Where did you get wiringPi?

The correct way to get it now is via GIT:

cd
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
gordon@drogon.net
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: wiringPi undefined Reference

Sat Jan 05, 2013 4:03 pm

IanH2 wrote: Some C libraries have 'C++ friendly' header files which do this automatically, but it looks like wiringPi isn't one of them (yet).
witingPi is one of them and has been for a long time now.

something else is going on - possibly not having the right linker flags. Just as you need -lm to include the maths library, you need -lwiringPi to include the wiringPi library.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Ced
Posts: 22
Joined: Thu Jan 03, 2013 5:33 pm
Location: Martinique, FWI

Re: wiringPi undefined Reference

Sun Jan 06, 2013 1:27 am

gordon@drogon.net wrote:
pt254 wrote:thank you, there was no wrapper in the lib. after adding that i works.
wiringPi has had those wrappers for a very long time now. (6+ months) Where did you get wiringPi?

The correct way to get it now is via GIT:

cd
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

-Gordon
i install whith this way, but i have this message i compile :
"main.cpp:(.text.startup+0x147): undefined reference to `digitalRead'"

User avatar
gordon@drogon.net
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: wiringPi undefined Reference

Sun Jan 06, 2013 9:11 am

Ced wrote:
gordon@drogon.net wrote:
pt254 wrote:thank you, there was no wrapper in the lib. after adding that i works.
wiringPi has had those wrappers for a very long time now. (6+ months) Where did you get wiringPi?

The correct way to get it now is via GIT:

cd
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

-Gordon
i install whith this way, but i have this message i compile :
"main.cpp:(.text.startup+0x147): undefined reference to `digitalRead'"
How are you compiling/linking? You need to add -lwiringPi to the compile line.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Ced
Posts: 22
Joined: Thu Jan 03, 2013 5:33 pm
Location: Martinique, FWI

Re: wiringPi undefined Reference

Mon Jan 07, 2013 7:04 pm

i include wiringPi in the main

Code: Select all

#include <QCoreApplication>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <chauffage.h>
#include <eclairage.h>
#include <led_strip.h>
#include <pompe_air.h>
#include <pompe_eau.h>
#include <wiringPi.h>

User avatar
gordon@drogon.net
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: wiringPi undefined Reference

Mon Jan 07, 2013 7:19 pm

Ced wrote:i include wiringPi in the main

Code: Select all

#include <QCoreApplication>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <chauffage.h>
#include <eclairage.h>
#include <led_strip.h>
#include <pompe_air.h>
#include <pompe_eau.h>
#include <wiringPi.h>
That's good, but if you get an "undefined reference" error, then it means that the compiled program has not been linked with the wiringPi library.

If you are compiling by hand, then you need to add -lwiringPi to the command-line. If you are using some sort of IDE, then you must work out how to include the wiringPi library inside that IDE.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Ced
Posts: 22
Joined: Thu Jan 03, 2013 5:33 pm
Location: Martinique, FWI

Re: wiringPi undefined Reference

Mon Jan 07, 2013 10:18 pm

I solve my problem, for those who use qt creator. You must add a line in the file .pro

Code: Select all

LIBS += -L/usr/local/include -lwiringPi

photomankc
Posts: 74
Joined: Fri Aug 24, 2012 12:58 pm

Re: wiringPi undefined Reference

Sat Jan 26, 2013 6:32 am

I have not been able to get past the undefined reference errors either. I'm using Arch and installed the library via the package system. It compiles fine but fails to link. Below is the output from the make file:

Code: Select all

robot@robopi-1 battmond]$ make
g++ -Wall -std=c++0x -c  -c i2c.cpp -o build/i2c.o
g++ -Wall -std=c++0x -c  -c battmond.cpp -o build/battmond.o
battmond.cpp: In function 'int main()':
battmond.cpp:68:9: warning: unused variable 'status' [-Wunused-variable]
g++ -Wall -std=c++0x -c  -c ADC_MCP3221.cpp -o build/ADC_MCP3221.o
g++ -lwiringPi build/i2c.o build/battmond.o build/ADC_MCP3221.o -o ./bin/battmond
build/battmond.o: In function `configureGPIO()':
battmond.cpp:(.text+0x644): undefined reference to `wiringPiSetup'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
I'm at a loss to understand where I go from here?

photomankc
Posts: 74
Joined: Fri Aug 24, 2012 12:58 pm

Re: wiringPi undefined Reference

Sat Jan 26, 2013 6:59 am

Never mind. Discovered that it matters where the library directive is included. Had to change the "all" target to this:

Code: Select all

all: $(OBJECTS)
        $(LD) $(LDFLAGS) $(OBJECTS) -o $(BIN) -lwiringPi
so that the compile line generated became this:

Code: Select all

g++ -L/usr/lib build/i2c.o build/battmond.o build/ADC_MCP3221.o -o ./bin/battmond -lwiringPi
Then it compiled and linked correctly. Sorry, always seems like you figure it out 5 minutes after you post in frustration.

User avatar
gordon@drogon.net
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: wiringPi undefined Reference

Sat Jan 26, 2013 11:28 am

photomankc wrote: Then it compiled and linked correctly. Sorry, always seems like you figure it out 5 minutes after you post in frustration.
Yes, -lwiringPi is the trick. I've been re-writing some of the on-line documention for it, and have included this now. It's one of those obvious things if you've been doing it for the past 30 years, but...

https://projects.drogon.net/raspberry-p ... functions/

Also, if installing it on Arch, the system doesn't include /usr/local/lib by default, so you need to add that into /etc/ld.so.conf and then run sudo ldconfig

-Gordon
--
Gordons projects: https://projects.drogon.net/

JakouC
Posts: 1
Joined: Wed Dec 17, 2014 7:09 pm

Re: wiringPi undefined Reference

Wed Dec 17, 2014 7:18 pm

Solution is easier than it looks like,
You must think that library is necesary to compile, so if you compile it in your PC without the RPi compilator it never will work. So you have 2 options.
1.- Compile it in the raspberry (is easier) you only need to compile the libraries and then copy the .so to your libs directory
2.- Make another cross compile for this library in your pc.
I'm working in this way and I can cross debugging and execute it remotely.

Austin911
Posts: 1
Joined: Tue May 26, 2015 11:07 am

Re: wiringPi undefined Reference

Tue May 26, 2015 11:37 am

Hello, i had the same problem with the "Geany " editor.
" undifened reference " when build.
Just insert " -lwiringPi " in the " build " menu / " set build commands " / " build " command
it gives : gcc -Wall -lwiringPi -o "%e" "%f"
Thanks to Gordon for " WiringPi " :P

Nauti
Posts: 8
Joined: Fri Dec 09, 2016 12:20 am

Re: wiringPi undefined Reference

Fri Dec 09, 2016 12:25 am

Since I was struggling with this ridiculous task of compiling a lousy little file (nes.c) for nearly two hours, before a friend came to the rescue, I feel I should contribute to save others the frustration. I kept getting the error:
"
/tmp/ccYcuirL.o: In function `main':
nes.c:(.text+0x60): undefined reference to `setupNesJoystick'
nes.c:(.text+0x98): undefined reference to `readNesJoystick'
collect2: error: ld returned 1 exit status
"

Tried to solve it in many ways with no desireable result. Tried other files in the example folder with up to 30 errors. His solution was simple: "gcc -lwiringPi -lwiringPiDev nes.c" and it compiled straight away.

User avatar
gordon@drogon.net
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: wiringPi undefined Reference

Fri Dec 09, 2016 6:22 am

Nauti wrote:Since I was struggling with this ridiculous task of compiling a lousy little file (nes.c) for nearly two hours, before a friend came to the rescue, I feel I should contribute to save others the frustration. I kept getting the error:
"
/tmp/ccYcuirL.o: In function `main':
nes.c:(.text+0x60): undefined reference to `setupNesJoystick'
nes.c:(.text+0x98): undefined reference to `readNesJoystick'
collect2: error: ld returned 1 exit status
"

Tried to solve it in many ways with no desireable result. Tried other files in the example folder with up to 30 errors. His solution was simple: "gcc -lwiringPi -lwiringPiDev nes.c" and it compiled straight away.
Or, since there is a Makefile, just type:

Code: Select all

  make nes
-Gordon
--
Gordons projects: https://projects.drogon.net/

Lineaxe
Posts: 21
Joined: Fri Nov 06, 2015 1:16 pm

Re: wiringPi undefined Reference

Fri Dec 09, 2016 1:29 pm

I managed to get it all going with QT c++ . This gives you a drag & drop environment to create windows & widgets as well as interface with wiring pi GPIO . Are you using Eclipse or Visual Studio to create c++ instead of QT ? I Can't help hook into those IDE's :)

Akhilesh_Mehta
Posts: 1
Joined: Fri Mar 16, 2018 5:25 am

Re: wiringPi undefined Reference

Fri Mar 16, 2018 5:38 am

I am using Raspberry Pi Model B V1.1
i want to blink the LED with Raspberry Pi Board
i have installed wiringPi as per below link.


http://www.electronicwings.com/raspberr ... spberry-pi

When I am compiling the program as below :

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

#define LedPin 0

int main(void) {
if(wiringPiSetup() == -1) { //when initialize wiringPi failed, print message to screen
printf("setup wiringPi failed !\n");
return -1;
}

pinMode(LedPin, OUTPUT);
while(1) {
digitalWrite(LedPin, LOW); //led on
printf("led on\n");
delay(1000); // wait 1 sec
digitalWrite(LedPin, HIGH); //led off
printf("led off\n");
delay(1000); // wait 1 sec
}
return 0;
}

though i have inserted -lwiringPi in my make file. (Linked with Library)
It is showing Error messages like :


gcc -Wall -I.. -O3 -c -o SPI_LCD.o SPI_LCD.c
gcc -o SPI_LCD SPI_LCD.c -lwiringPi
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `crypt'
Makefile:22: recipe for target 'SPI_LCD' failed
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `rint'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `pthread_join'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `pthread_create'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `pow'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `shm_open'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `pthread_cancel'
collect2: error: ld returned 1 exit status
make: *** [SPI_LCD] Error 1

Please help me as soon as possible.

SteveSpencer
Posts: 332
Joined: Thu Mar 28, 2013 9:19 am
Location: Nottingham, UK

Re: wiringPi undefined Reference

Fri Mar 16, 2018 11:41 am

As well as -lwiringpi, you need -lpthread -lcrypt -lm, as these undefined references are in those libraries, respectively.
Steve S
No, I can't think of anything funny that won't offend someone if they want it to...

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 10 guests