RonMexico
asked on
'Undefined reference' when compiling my sample C code
So, I am experimenting with a Raspberry Pi computer, fairly inexperienced in linux but lots of windows and programming experience. I'm following these instructions to get my computer talking MODBUS over ethernet.
http://www.ostafichuk.com/raspberry-pi-projects/modbus-on-the-pi/
Basically I ran "sudo apt-get install libmodbus5 libmodbus-dev" which seemed to execute okay (to my novice eye) lots of feedback but no errors or warnings.
Then I wrote some sample code which has #include "modbus.h" and calls functions which (according to the documentation) should be in modbus_new_tcp(), etc. Then I ran the recommended compiling command:
but first it had trouble finding the modbus.h file. So I found the file in /usr/include and I included (after google searching) the parameter to my command:
and it found the include file but reported a bunch of "undefined reference" errors for my modbus calls such as modbus_new_tcp.
Then I found the library (I think) in /user/lib, specifically these files:
/usr/lib/libmodbus.so (shortcut)
/usr/lib/libmodbus.so.5 (shortcut)
/usr/lib/libmodbus.so.5.0. 2
So I added the -L option as follows:
and I still got the undefined reference errors.
So it appears that the "apt-get" command didn't do EVERYTHING I needed to do to start using this library, or I am executing the compiler incorrectly.
Can any of you linux experts please give me some pointers?
I really appreciate any thoughts.
http://www.ostafichuk.com/raspberry-pi-projects/modbus-on-the-pi/
Basically I ran "sudo apt-get install libmodbus5 libmodbus-dev" which seemed to execute okay (to my novice eye) lots of feedback but no errors or warnings.
Then I wrote some sample code which has #include "modbus.h" and calls functions which (according to the documentation) should be in modbus_new_tcp(), etc. Then I ran the recommended compiling command:
$gcc bandwidth-server-one.c -o bandwidth-server-one `pkg-config –libs –cflags libmodbus`
but first it had trouble finding the modbus.h file. So I found the file in /usr/include and I included (after google searching) the parameter to my command:
$gcc bandwidth-server-one.c -o bandwidth-server-one `pkg-config –libs –cflags libmodbus` -I/usr/include/modbus
and it found the include file but reported a bunch of "undefined reference" errors for my modbus calls such as modbus_new_tcp.
Then I found the library (I think) in /user/lib, specifically these files:
/usr/lib/libmodbus.so (shortcut)
/usr/lib/libmodbus.so.5 (shortcut)
/usr/lib/libmodbus.so.5.0.
So I added the -L option as follows:
$gcc bandwidth-server-one.c -o bandwidth-server-one `pkg-config –libs –cflags libmodbus` -I/usr/include/modbus -L/usr/lib
and I still got the undefined reference errors.
So it appears that the "apt-get" command didn't do EVERYTHING I needed to do to start using this library, or I am executing the compiler incorrectly.
Can any of you linux experts please give me some pointers?
I really appreciate any thoughts.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
After you fix the pkg-config invocation, you should probably revert to the first compile line that you posted.
ASKER
Thanks!! There was no output actually, aside from my executable appearing in the folder, which was a beautiful sight...
Man, been in the software industry 15 years... don't like being so freaking novice again.
Man, been in the software industry 15 years... don't like being so freaking novice again.
You should code -lmodbus to pick up libmodbus.so from the standard locations (/lib and /usr/lib). (-l prepends lib to whatever follows).