poilkjewq
Posts: 16
Joined: Fri Jun 15, 2018 9:50 am

Having problem on C/C++ library wiringSerial

Mon Jun 18, 2018 9:00 am

Hello everyone! I am a beginner of raspberry pi.

I am trying to write a command to the serial device then getting back the response. For example, I send "SN" to the serial device and then the serial device send back the serial number to my computer.

In my project, my connection of the device and raspberry pi like this:
[Serial Device]===[usb to serial db9 adaptor]===[raspberry pi]===[Ethernet]===[Computer]

When I compile the following code, I have the problem which shown below the code.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <wiringPi.h>
#include <wiringSerial.h>

int main ()
{
  int serial_port ;
  char dat;
  if ((serial_port = serialOpen ("/dev/ttyUSB0", 9600)) < 0)	/* open serial port */
  {
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

  if (wiringPiSetup () == -1)					/* initializes wiringPi setup */
  {
    fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
    return 1 ;
  }

  while(1){
	  
	if(serialDataAvail (serial_port) )
	{ 
		dat = serialGetchar (serial_port);		/* receive character serially*/	
		printf ("%c", dat) ;
		fflush (stdout) ;
		serialPutchar(serial_port, dat);		/* transmit character serially on port */
		  }
	}

}

Code: Select all

[email protected]:~ $ g++ tcp.c -o 123
/tmp/ccAn5dfs.o: In function 'main':
tcp.c:(/text+0x14): undefined referce to 'serialOpen'
tcp.c:(/text+0x60): undefined referce to 'WiringPiSetup'
tcp.c:(/text+0xac): undefined referce to 'serialDataAvail'
tcp.c:(/text+0xc0): undefined referce to 'serialGetchar'
tcp.c:(/text+0xf4): undefined referce to 'serialPutchar'
collect2: error: ld returned 1 exit status
Since I am using usb to serial db9 adatpor, do I need to any configure on ttyUSB0 port?
If yes, can u provide some possible solution for me? Thanks :D

User avatar
topguy
Posts: 4738
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Having problem on C/C++ library wiringSerial

Mon Jun 18, 2018 12:07 pm

"undefined references" usually means you are not linking with the correct library. GCC does not try to find out by itself which of the hundreds of installed libraries it should use. You have to tell GCC to link with the WiringPi library.

Code: Select all

g++ tcp.c -o 123 -lwiringPi

poilkjewq
Posts: 16
Joined: Fri Jun 15, 2018 9:50 am

Re: Having problem on C/C++ library wiringSerial

Tue Jun 19, 2018 8:21 am

topguy wrote:
Mon Jun 18, 2018 12:07 pm
"undefined references" usually means you are not linking with the correct library. GCC does not try to find out by itself which of the hundreds of installed libraries it should use. You have to tell GCC to link with the WiringPi library.

Code: Select all

g++ tcp.c -o 123 -lwiringPi
Thank you for your reply. I can compile the c file now. :D :D

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 11 guests