jgarff / rpi_ws281x

Userspace Raspberry Pi PWM library for WS281X LEDs
BSD 2-Clause "Simplified" License
1.76k stars 616 forks source link

undefined reference to 'ws2811_...' #478

Open R3tr0BoiDX opened 2 years ago

R3tr0BoiDX commented 2 years ago

I did installed the library as described in the README by doing

cd build
cmake -D BUILD_SHARED=ON -D BUILD_TEST=OFF ..
cmake --build .
sudo make install

==> It says:

[100%] Built target ws2811
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib/libws2811.so
-- Installing: /usr/local/include/ws2811/ws2811.h
-- Installing: /usr/local/include/ws2811/rpihw.h
-- Installing: /usr/local/include/ws2811/pwm.h
-- Installing: /usr/local/include/ws2811/clk.h
-- Installing: /usr/local/include/ws2811/dma.h
-- Installing: /usr/local/include/ws2811/gpio.h
-- Installing: /usr/local/include/ws2811/mailbox.h
-- Installing: /usr/local/include/ws2811/pcm.h
-- Installing: /usr/local/lib/pkgconfig/libws2811.pc

However, if I want to compile my code with gcc main.c -o test, I get main.c:(.text+0x18): undefined reference to 'ws2811_fini' for example.

My main.c: #include <ws2811/ws2811.h> int main(int argc, char const *argv[]) { return 0; }

Gadgetoid commented 2 years ago

The header file "declares" all the functions you're using, but the object file or library is responsible for "defining" them.

You need to add -lws2811 to instruct gcc to link against the ws2812 library and include the definitions of ws2811_fini. In the simplest sense this either smooshes your code and the library code together into a single executable (static linking) or allows gcc to create a binary that knows where to find the library file dynamic linking).

So:

gcc main.c -lws2811 -o test
KC5-BP commented 2 years ago

Hello LED pals ;)

In advance, if you encounter that issue after linking the -lws2811 ('cause I did) : $ gcc main.c -lws2811 -o test /usr/bin/ld: /usr/local/lib/libws2811.a(ws2811.c.o): in function ws2811_set_custom_gamma_factor: ws2811.c:(.text+0x2934): undefined reference to pow collect2: error: ld returned 1 exit status

Just link the math lib. into the cmd as follow : $ gcc main.c -lws2811 -lm -o test