Link to home
Start Free TrialLog in
Avatar of RonMexico
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:

$gcc bandwidth-server-one.c -o bandwidth-server-one `pkg-config –libs –cflags libmodbus`

Open in new window


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

Open in new window


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:

$gcc bandwidth-server-one.c -o bandwidth-server-one `pkg-config –libs –cflags libmodbus` -I/usr/include/modbus -L/usr/lib

Open in new window


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.
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

The -I option wants a directory, not a file. So you would code -I /usr/include, except that if you need to do that then it would seem the compiler was not properly installed because /usr/include is the default.

You should code -lmodbus to pick up libmodbus.so from the standard locations (/lib and /usr/lib). (-l prepends lib to whatever follows).
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After you fix the pkg-config invocation, you should probably revert to the first compile line that you posted.
Avatar of RonMexico
RonMexico

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.