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.
LinuxCLinux NetworkingRaspberry Pi

Avatar of undefined
Last Comment
RonMexico

8/22/2022 - Mon
Duncan Roe

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
Duncan Roe

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Duncan Roe

After you fix the pkg-config invocation, you should probably revert to the first compile line that you posted.
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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck