-2

I am trying to work with the gpio_lib of cubietruck. Therefore I got the gpio_lib.h (see https://github.com/youyudehexie/node-cubieboard-gpio/blob/master/lib/gpio_lib.h) file located in my working directory:

> l
gpio_lib.h
test.cpp

In my test.cpp I have the following include:

#include "gpio_lib.h"

and some calls of the gpio_lib functions like sunxi_gpio_init and sunxi_gpio_output within the main() function.

However if I try to compile and link the send.cpp via

> g++ -o test -O3 -Wall test.cpp

I get

/tmp/ccULAi2f.o: In function `sequence_up()':
test.cpp:(.text+0x26a): undefined reference to `sunxi_gpio_output(unsigned int, unsigned int)'
/tmp/ccULAi2f.o:test.cpp:(.text.startup+0xa2): more undefined references to `sunxi_gpio_output(unsigned int, unsigned int)' follow
collect2: error: ld returned 1 exit status

I also tried following calls getting the same error:

> g++ -o test -O3 -Wall gpio_lib.h test.cpp
> g++ -o test -O3 -Wall -I. -L. gpio_lib.h test.cpp

What is the correct way to compile and link the test.cpp?

phortx
  • 849
  • 5
  • 21

3 Answers3

4

Compile and link the corresponding source file

g++ -o test test.cpp gpio_lib.c

Since the header is pure C, you'll need to add the language linkage yourself if you include it from C++:

extern "C" {
#include "gpio_lib.h"
}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

You cannot link with a header file. The error you are getting is linking error, i.e. function declaration was available in .h file, so it could compile, but definition of function is missing. You need to locate the correct library and link with that

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
2

By inclusion of header #include "gpio_lib.h" you have brought probably some declarations of functions. Since the header is in C you should add a linkage specifier before #include definition

extern "C" {
    #include "gpio_lib.h"
}

to provide appropriate linkage. Then you have to link to the binaries that are the definitions (the body) of these functions as well or ask the gcc (g++ is gcc module) to do all the things for you:

g++ -o test test.cpp gpio_lib.c
4pie0
  • 29,204
  • 9
  • 82
  • 118