troubleshooting Question

'Undefined reference' when compiling my sample C code

Avatar of RonMexico
RonMexico asked on
LinuxCLinux NetworkingRaspberry Pi
4 Comments1 Solution2524 ViewsLast Modified:
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.

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/ (shortcut)
/usr/lib/ (shortcut)

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.
Duncan Roe
Software Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros