Extremely simple C code development

Applications, development tools, FPGA, C, WEB
Post Reply
ttemsk
Posts: 10
Joined: Sun Jul 19, 2015 8:04 pm

Extremely simple C code development

Post by ttemsk » Mon Mar 27, 2017 6:25 pm

Colleagues,

I'd like to keep my R.P. setup as simple and independent as possible, and avoid hooking it to the Internet. For the purpose, I experimented with the C compiler included in the 0.97 ecosystem. I wrote the Hello World with the nano editor over an USB console connection, which compiled and executed from the command line just fine.

Complications began when trying to access hardware, which requires using the API. So I wrote the blink led C example with the nano editor, replacing the

Code: Select all

#include "redpitaya/rp.h"
line temporarily with the full path of the header file on the R.P. board:

Code: Select all

#include "/opt/redpitaya/include/redpitaya/rp.h
This compiles, but it seems the linker doesn't find the library to link against:

Code: Select all

In function 'main'
ledblink.c:(.text+0x3e): undefined reference to 'rp_Init'
ledblink.c:(.text+0x70): undefined reference to 'rp_DpinSetState'
ledblink.c:(.text+0x82): undefined reference to 'rp_DpinSetState'
ledblink.c:(.text+0x9a): undefined reference to 'rp_Release'
collect2: error: ld returned 1 exit status
Now, I don't seem to be able to set the library path right.

Code: Select all

root@rp-f018cb: LD_LIBRARY_PATH=/opt/redpitaya/lib
root@rp-f018cb: echo $LD_LIBRARY_PATH
/opt/redpitaya/lib
root@rp-f018cb: gcc ledblink.c
... same undefined reference errors
A symbol dump suggests that the library is there, and should contain the required functions:

Code: Select all

root@rp-f018cb: nm -D /opt/redpitaya/lib/librp.so
... a lot of lines ...
00007ff6 T rp_Init
... a lot of lines ...
Attempts to guide LD explicitly to the library fail as well:

Code: Select all

root@rp-f018cb: gcc -L /opt/redpitaya/lib ledblink.c
...same undefined reference errors

Code: Select all

root@rp-f018cb: gcc --library /opt/redpitaya/lib/librp.so ledblink.c
...same undefined reference errors
... and some other option variants with the same end result.

What am I missing? Such a simple LED blinking app should be compile-able from the command line without all the heavyweight wrestling with github cloning and makefiles, no?

Regards,
Mikko

User avatar
redpitaya
Site Admin
Posts: 883
Joined: Wed Mar 26, 2014 7:04 pm

Re: Extremely simple C code development

Post by redpitaya » Wed Mar 29, 2017 9:22 am

Hi Mikko,
have you followed these instructions?
https://redpitaya.readthedocs.io/en/lat ... plications
kind regards, Red Pitaya team

ttemsk
Posts: 10
Joined: Sun Jul 19, 2015 8:04 pm

Re: Extremely simple C code development

Post by ttemsk » Wed Mar 29, 2017 10:21 am

No, I haven't followed the examples, because I'd prefer not to go through the trouble of connecting my R.P. boards to Internet. Internet connection is needed if one wants to clone the repository, and it *did* seem to me that the necessary headers and libraries are already found in the unappended Ecosystem as

/opt/redpitaya/include/redpitaya/rp.h
and
/opt/redpitaya/lib/librp.so

In the meanwhile I found the related discussion thread viewtopic.php?f=8&t=1465 . If I cannot make it work with the bare Ecosystem, and by studying the switches used in the thread, I'll bite the bullet and clone the repository as suggested. It's enough to get one copy of the development version libraries to the /root/RedPitaya/api I guess, then I can just clone the SD cards.

Thanks for your help.

Regards,
Mikko

ttemsk
Posts: 10
Joined: Sun Jul 19, 2015 8:04 pm

Re: Extremely simple C code development

Post by ttemsk » Wed Mar 29, 2017 7:58 pm

OK, now I have cloned the repository and it is found under /root/RedPitaya . I have also loaded the API-specific FPGA bitstream.

The first odd thing is that the version of LED blinker found in the Examples/C subdirectory fails:

Code: Select all

root@rp-f018cb:~/RedPitaya/Examples/C# make digital_led_blink
cc -g -std=gnu99 -Wall -Werror -I/opt/redpitaya/include -L/opt/redpitaya/lib digital_led_blink.c -lm -lpthread -lrp -o digital_led_blink
root@rp-f018cb:~/RedPitaya/Examples/C# ./digital_led_blink
./digital_led_blink: error while loading shared libraries: librp.so: cannot open shared object file: No such file or directory
root@rp-f018cb:~/RedPitaya/Examples/C# ls -l /opt/redpitaya/lib/librp.so
-rwxr-xr-x 1 root root 38688 Feb 23 11:22 /opt/redpitaya/lib/librp.so
root@rp-f018cb:~/RedPitaya/Examples/C# 
The second odd thing is that the Makefile seems to attempt linking the code with the same includes and the same library I experimented with earlier, getting the unresolved references. The same still happens:

Code: Select all

root@rp-f018cb:~# ls -l
-rw-r--r-- 1 root root    616 Feb 24 00:08 ledblink.c
drwxr-xr-x 20 root root 4096 Mar 29 17:34 RedPitaya
root@rp-f018cb:~# cc -g -std=gnu99 -Wall -Werror -I/opt/redpitaya/include -L/opt/redpitaya/lib -lm -lpthread -lrp -o ledblink ledblink.c
/tmp/cc6C2p62.o: In function 'main':
/root/ledblink.c:18: undefined reference to 'rp_Init'
/root/ledblink.c:25: undefined reference to 'rp_DpinSetState'
/root/ledblink.c:27: undefined reference to 'rp_DpinSetState'
/root/ledblink.c:31: undefined reference to 'rp_Release'
collect2: error: ld returned 1 exit status
root@rp-f018cb:~#
The command line BTW had the same options as used by Nils Roos in the thread viewtopic.php?f=8&t=1465, except that he used /root/RedPitaya/api/include/ for includes and /root/RedPitaya/api/lib/ for libraries (thanks to him for the example). Those directories probably were for an older code version, in the current repository the RedPitaya/api does not contain a lib/ subdirectory.

I'm puzzled as to what I am missing. Could the librp.so file be broken somehow? But the

Code: Select all

nm -D /opt/redpitaya/lib/librp.so
dumps the symbols just fine.

User avatar
redpitaya
Site Admin
Posts: 883
Joined: Wed Mar 26, 2014 7:04 pm

Re: Extremely simple C code development

Post by redpitaya » Thu Mar 30, 2017 12:39 pm

Hi,
we can confirm that instructions in our doc. work (http://redpitaya.readthedocs.io/en/late ... /comC.html)
Test was done on STEMlab OS:
# Red Pitaya GNU/Linux Ecosystem
# Version: 0.97
# Build: 336
root@rp-f04cee:~/RedPitaya/Examples/C# make digital_led_blink
cc -g -std=gnu99 -Wall -Werror -I/opt/redpitaya/include -L/opt/redpitaya/lib digital_led_blink.c -lm -lpthread -lrp -o digital_led_blink
root@rp-f04cee:~/RedPitaya/Examples/C# cat /opt/redpitaya/fpga/fpga_0.94.bit > /dev/xdevcfg
root@rp-f04cee:~/RedPitaya/Examples/C# LD_LIBRARY_PATH=/opt/redpitaya/lib ./digital_led_blink
Blinking LED[0]
^C
root@rp-f04cee:~/RedPitaya/Examples/C
Please first try to follow the instructions and then try to do modifications so you see where the problem is.
kind regards, Red Pitaya team

ttemsk
Posts: 10
Joined: Sun Jul 19, 2015 8:04 pm

Re: Extremely simple C code development

Post by ttemsk » Thu Mar 30, 2017 12:48 pm

Some progress ... I'm able to compile and run the examples in the cloned repository now. The problem was what has bitten me before: the variable LD_LIBRARY_PATH is only visible to the shell if it is not exported. The dynamic linker obviously needs to know the library location at the time it launches the executable. The line

Code: Select all

root@rp-f018cb:~# export LD_LIBRARY_PATH=/opt/redpitaya/lib
does the trick.

Still puzzled about the unresolvable references, in case I'm trying to be independent of the repository ...

Post Reply
jadalnie klasyczne ekskluzywne meble wypoczynkowe do salonu ekskluzywne meble tapicerowane ekskluzywne meble do sypialni ekskluzywne meble włoskie

Who is online

Users browsing this forum: No registered users and 14 guests