Compilation issue

Asked by JM

Hi,
  I am using a ARM based development platform. I have made sure modbus.h exists in the usr/include folder. I am trying to communicate with a plc which is a modbus slave through a C program. The program just opens and closes the connection as I basically wanted to test this function first before I proceeded any further:

# include "stdio.h"
# include "stdlib.h"
# include "modbus/modbus.h"

#define plc 0x01

int main(void)
{
modbus_param_t mb_param;

modbus_init_rtu(&mb_param,"/dev/ttyS2","none",8,1);

if (modbus_connect(&mb_param) == -1) {
printf("Not connected\n");
exit(1);
}

modbus_close(&mb_param);

}

When I compile this program, I am faced with errors:

undefined reference to 'modbus_init_rtu'
undefined reference to 'modbus_connect'
undefined reference to 'modbus_close'

I have doubled checked that these functions do exist in the header file.

I would very much appreciate it if someone could let me know what is wrong with my program and can suggest a solution.

Best Regards,
JM

Question information

Language:
English Edit question
Status:
Solved
For:
libmodbus Edit question
Assignee:
No assignee Edit question
Solved by:
JM
Solved:
Last query:
Last reply:
Revision history for this message
Stéphane Raimbault (sra) said :
#1

Could you use the 3.0.X series, please?

Revision history for this message
JM (bluelotus256) said :
#2

Hi Stephane,
                    I upgraded the libmodbus package to 3.0.1 and I am using bitbake to package my application. Here is the bitbake recipe:
        DESCRIPTION = "modbuscomm"
SECTION = "examples"
LICENSE = "GPL"
DEPENDS = "libesmtp libyaml"
PR = "r12"

SRC_URI = "file://mymodbuscomm.c \
file://modbus.c \
file://modbus.h \
file://modbus-data.c \
file://modbus-rtu.c \
file://modbus-rtu.h \
file://modbus-tcp.c \
file://modbus-tcp.h"

S = "${WORKDIR}"

do_compile() {
${CC} ${CFLAGS} ${LDFLAGS} mymodbuscomm.c -o mymodbuscomm
${CC} ${CFLAGS} ${LDFLAGS} modbus.c -o modbus.c
${CC} ${CFLAGS} ${LDFLAGS} modbus-data.c -o modbus-data.c
${CC} ${CFLAGS} ${LDFLAGS} modbus-rtu.c -o modbus-rtu.c
${CC} ${CFLAGS} ${LDFLAGS} modbus-tcp.c -o modbus-tcp.c

}

do_install() {
install -d ${D}${bindir}
install -m 0755 mymodbuscomm ${D}${bindir}
install -m 0755 modbus ${D}${bindir}
install -m 0755 modbus-data ${D}${bindir}
install -m 0755 modbus-rtu ${D}${bindir}
install -m 0755 modbus-tcp ${D}${bindir}

}

After I proceeded to bitbake the file I was faced with these errors:

 mymodbuscomm.c: In function 'main':
| mymodbuscomm.c:9: error: 'modbus_param_t' undeclared (first use in this function)
| mymodbuscomm.c:9: error: (Each undeclared identifier is reported only once
| mymodbuscomm.c:9: error: for each function it appears in.)
| mymodbuscomm.c:9: error: expected ';' before 'mb_param'
| mymodbuscomm.c:12: error: 'mb_param' undeclared (first use in this function)
| ERROR: Function do_compile failed

Having gone through the other threads that reported similar problems I gathered and made sure all relevant header files and the src folder .c files existed in the same folder as my program. Any further suggestions on how to go about solving these errors would be very much helpful. Thank you and have a great day.
                                                                                                                              Best Regards,
                                                                                                                              JM

Revision history for this message
JM (bluelotus256) said :
#3

Sorry I had to post my bitbake recipe once more as there was a small typo in the previous post:

DESCRIPTION = "modbuscomm"
SECTION = "examples"
LICENSE = "GPL"
PR = "r12"

SRC_URI = "file://mymodbuscomm.c \
file://modbus.c \
file://modbus.h \
file://modbus-data.c \
file://modbus-rtu.c \
file://modbus-rtu.h \
file://modbus-tcp.c \
file://modbus-tcp.h"

S = "${WORKDIR}"

do_compile() {
${CC} ${CFLAGS} ${LDFLAGS} mymodbuscomm.c -o mymodbuscomm
${CC} ${CFLAGS} ${LDFLAGS} modbus.c -o modbus
${CC} ${CFLAGS} ${LDFLAGS} modbus-data.c -o modbus-data
${CC} ${CFLAGS} ${LDFLAGS} modbus-rtu.c -o modbus-rtu
${CC} ${CFLAGS} ${LDFLAGS} modbus-tcp.c -o modbus-tcp

}

do_install() {
install -d ${D}${bindir}
install -m 0755 mymodbuscomm ${D}${bindir}
install -m 0755 modbus ${D}${bindir}
install -m 0755 modbus-data ${D}${bindir}
install -m 0755 modbus-rtu ${D}${bindir}
install -m 0755 modbus-tcp ${D}${bindir}

}

  Thanks,
   JM

Revision history for this message
Karthik.K.M. (karthikkm1987) said :
#4

in the gcc (example:- arm-linux-gcc)compiler you are using where ever stdio.h header files are there put all the modbus related header files.i think it might compile then.It worked for me though.

example:-/home/karthik/porting/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/include/c++/4.4.3/tr1/
/home/karthik/porting/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/sys-root/usr/include/
/home/karthik/porting/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/sys-root/usr/include/bits/
/home/karthik/porting/opt/FriendlyARM/toolschain/4.4.3/lib/gcc/arm-none-linux-gnueabi/4.4.3/include/ssp/

for me when i put modbus.h,modbus-tcp.h,... in the above directories it got compiled.
also
do this as shown i mean include all the c files of libmodbus in the command.:-
arm-linux-gcc -o modclient modclient3.c /home/karthik/project2/libmodbus-3.0.1/src/modbus.c /home/karthik/project2/libmodbus-3.0.1/src/modbus-tcp.c /home/karthik/project2/libmodbus-3.0.1/src/modbus-rtu.c /home/karthik/project2/libmodbus-3.0.1/src/modbus-data.c

Revision history for this message
Stéphane Raimbault (sra) said :
#5

The example code you try to compile doesn't use the API of libmodbus 3.0.X

Revision history for this message
JM (bluelotus256) said :
#6

Hi Karthik,
                  I did copy all the relevant header files to the usr/include/modbus directory as well as including all c files of the libmodus in my bitbake recipe however that did not solve the issue. I am still stuck with the same errors.
                                                                                                                                                                                         Best Regards,
                                                                                                                                                                                        JM

Revision history for this message
Karthik.K.M. (karthikkm1987) said :
#7

Not just that.Whereever the gcc of arm you have extracted you know you have to put there also.If it doesnt work try for instance copying the modbus headerfiles into the directory where your program is present.

Also check the version of libmodbus you are using.I am using libmodbus-3.0.1 version.

Revision history for this message
JM (bluelotus256) said :
#8

Hi Karthik,
                  As far as I know I have included all modbus header files in to the gcc include folders. Also I tried moving them to the folder where my program exists along with the relevant .c files. The version of libmodbus is indeed 3.0.1. While I appreciate your help I would like to know if there is something specifically wrong with my bitabke recipe that I posted recently (please check my earlier posts). I would very much appreciate it if you have any other suggestions as the errors are still cropping up.
                                                                                                                                                                                                                   Thanks,
                                                                                                                                                                                                                   JM

Revision history for this message
JM (bluelotus256) said :
#9

Hi,
     I would appreciate it if someone who has worked on an ARM Platform and used bitbake to build their applications and ran in to similar errors which I posted in my earlier posts to let me know what changes are required in my bitbake recipe or suggest me anything else that might solve these errors. Thank you very much.
                                                                                                                             Regards,
                                                                                                                             JM

Revision history for this message
JM (bluelotus256) said :
#10

Hi,
   Looks like I goofed up installing libmodbus. I'll post again when I uninstall and re-install libmodbus and get it to work. Thanks for your help everyone.
                                       Regards,
                                       JM

Revision history for this message
JM (bluelotus256) said :
#11

Hi,
   I have installed libmodbus again. When I tried to compile the example program bandwidth-server-one.c I was faced with these errors:

Log data follows:
| /tmp/cckmSANb.o: In function `main':
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:56: undefined reference to `modbus_new_tcp'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:57: undefined reference to `modbus_tcp_listen'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:58: undefined reference to `modbus_tcp_accept'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:66: undefined reference to `modbus_mapping_new'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:80: undefined reference to `modbus_reply'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:78: undefined reference to `modbus_receive'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:87: undefined reference to `modbus_strerror'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:89: undefined reference to `modbus_mapping_free'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:91: undefined reference to `modbus_free'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:61: undefined reference to `modbus_new_rtu'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:62: undefined reference to `modbus_set_slave'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:63: undefined reference to `modbus_connect'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:69: undefined reference to `modbus_strerror'
| /home/jm/Desktop/corecdp-1.1.1/build/tmp/work/armv5te-corecdp-linux-gnueabi/modbuscomm-1.0.0-r69/bandwidth-server-one.c:71: undefined reference to `modbus_free'

In the usr/include/modbus folder I made sure that I changed 'include"modbus.h"' to 'include"modbus/modbus.h" 'in the file bandwidth-server-one.c as well as modbus.h,modbus-rtu.h,modbus-tcp.h as the header files have been installed in a folder called 'modbus' in the usr/include directory. Somehow the modbus-rtu.h and the modbus-tcp.h don't seem to be getting included though there are include statements in modbus.h.

I would appreciate any suggestions on this one. Thank you very much and have a great day.
                                                                                                                                                               Best regards,
                                                                                                                                                               JM

Revision history for this message
JM (bluelotus256) said :
#12

Hi,
   the issue has been resolved. The problem was that the arm cross compiler was not compiling against the libmodbus library.
                                                                                                                                                                                                               Best Regards,
                                                                                                                                                                                                               JM

Revision history for this message
JohnMajor (synenergy) said :
#13

Hi JM

I came across you post - I am having exactly the same problems with compiling libmodbus (3.03) using eclipse for the arm?

You mentioned that the problem was that the arm cross compiler was not compiling against the libmodbus library - but never posted you solution on how to solve this problem?
Any help would be much appreciated
regards
John

Revision history for this message
JM (bluelotus256) said :
#14

Dear John,
              Its been a long while since I worked with the ARM. That was a
Multitech developer platform. I will try to figure out what solution was it
that I found back then. Meanwhile I posted on Multitech Developer platform
at the time as well. Please go though these posts maybe they would be of
help.

http://www.multitech.net/developer/forums/topic.php?id=126

Sorry as right now I do not exactly remember the solution.I hope to find it
and get back to you soon. Take care and good luck.

                                                        Regards,

                                                        JM

On Mon, May 27, 2013 at 6:36 PM, JohnMajor <
<email address hidden>> wrote:

> Your question #172535 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/172535
>
> JohnMajor posted a new comment:
> Hi JM
>
> I came across you post - I am having exactly the same problems with
> compiling libmodbus (3.03) using eclipse for the arm?
>
> You mentioned that the problem was that the arm cross compiler was not
> compiling against the libmodbus library - but never posted you solution on
> how to solve this problem?
> Any help would be much appreciated
> regards
> John
>
> --
> You received this question notification because you asked the question.
>

Revision history for this message
JohnMajor (synenergy) said :
#15

Much Appreciated John,

I've been racking my brains last few days trying to get this to work?

I am using eclipse indigo platform (on a Ubuntu PC) and have used the
libmodbus (libmodbus-3.0.3) library for compilation on X86 c/c++ projects -
these work OK!

But unfortunately I can't get it to work with the "arm-linux-gnueanihf-gcc"
compiler I am using.
This arm compiler works fine under normal circumstances but when I use the
libmodus library it throws the following errors:

1. cannot find -lmodbus
2. skipping incompatible /usr/local/lib/libmodbus.so when searching
for -lmodbus

Obviously it seems to be finding the libraries in the right place OK but
thinks its incompatible for some reason?
Pretty sure the libraries and include files are located in the right places?

any help would be very much appreciated!
regards
John Major

-----Original Message-----
From: JM
Sent: Tuesday, May 28, 2013 12:56 PM
To: <email address hidden>
Subject: Re: [Question #172535]: Compilation issue

Question #172535 on libmodbus changed:
https://answers.launchpad.net/libmodbus/+question/172535

JM posted a new comment:
Dear John,
              Its been a long while since I worked with the ARM. That was a
Multitech developer platform. I will try to figure out what solution was it
that I found back then. Meanwhile I posted on Multitech Developer platform
at the time as well. Please go though these posts maybe they would be of
help.

http://www.multitech.net/developer/forums/topic.php?id=126

Sorry as right now I do not exactly remember the solution.I hope to find it
and get back to you soon. Take care and good luck.

                                                        Regards,

                                                        JM

On Mon, May 27, 2013 at 6:36 PM, JohnMajor <
<email address hidden>> wrote:

> Your question #172535 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/172535
>
> JohnMajor posted a new comment:
> Hi JM
>
> I came across you post - I am having exactly the same problems with
> compiling libmodbus (3.03) using eclipse for the arm?
>
> You mentioned that the problem was that the arm cross compiler was not
> compiling against the libmodbus library - but never posted you solution on
> how to solve this problem?
> Any help would be much appreciated
> regards
> John
>
> --
> You received this question notification because you asked the question.
>

--
You received this question notification because you are a direct
subscriber of the question.

Revision history for this message
JohnMajor (synenergy) said :
#16

JM
The problem was found to be due to the compiler not linking correctly with
libmodbus library using eclipse running from my X86 based development
desktop pc?
Anyway to solve the issue, I installed libmodbus on an arm-based pc running
ubuntu/linux and then copied all the libmodbus files from the arm based pc
to an special folder on the X86 based development desktop. After setting the
appropriate directory these new files have allow the arm-linux-gnueanihf-gcc
compiler to do its job and now accepts the new libmodbus libraries and links
OK
John Major

-----Original Message-----
From: JM
Sent: Tuesday, May 28, 2013 12:56 PM
To: <email address hidden>
Subject: Re: [Question #172535]: Compilation issue

Question #172535 on libmodbus changed:
https://answers.launchpad.net/libmodbus/+question/172535

JM posted a new comment:
Dear John,
              Its been a long while since I worked with the ARM. That was a
Multitech developer platform. I will try to figure out what solution was it
that I found back then. Meanwhile I posted on Multitech Developer platform
at the time as well. Please go though these posts maybe they would be of
help.

http://www.multitech.net/developer/forums/topic.php?id=126

Sorry as right now I do not exactly remember the solution.I hope to find it
and get back to you soon. Take care and good luck.

                                                        Regards,

                                                        JM

On Mon, May 27, 2013 at 6:36 PM, JohnMajor <
<email address hidden>> wrote:

> Your question #172535 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/172535
>
> JohnMajor posted a new comment:
> Hi JM
>
> I came across you post - I am having exactly the same problems with
> compiling libmodbus (3.03) using eclipse for the arm?
>
> You mentioned that the problem was that the arm cross compiler was not
> compiling against the libmodbus library - but never posted you solution on
> how to solve this problem?
> Any help would be much appreciated
> regards
> John
>
> --
> You received this question notification because you asked the question.
>

--
You received this question notification because you are a direct
subscriber of the question.

Revision history for this message
JM (bluelotus256) said :
#17

Dear John,
              you are perfectly correct. Now when I remember, I faced the
same problem of the complier not having linked with the libmodbus library.
Glad that you found the solution. Thank you for refreshing my memory :)
Take care and Good Luck.

   Regards,

   JM

On Wed, May 29, 2013 at 8:01 PM, JohnMajor <
<email address hidden>> wrote:

> Your question #172535 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/172535
>
> JohnMajor posted a new comment:
> JM
> The problem was found to be due to the compiler not linking correctly with
> libmodbus library using eclipse running from my X86 based development
> desktop pc?
> Anyway to solve the issue, I installed libmodbus on an arm-based pc running
> ubuntu/linux and then copied all the libmodbus files from the arm based pc
> to an special folder on the X86 based development desktop. After setting
> the
> appropriate directory these new files have allow the
> arm-linux-gnueanihf-gcc
> compiler to do its job and now accepts the new libmodbus libraries and
> links
> OK
> John Major
>
>
> -----Original Message-----
> From: JM
> Sent: Tuesday, May 28, 2013 12:56 PM
> To: <email address hidden>
> Subject: Re: [Question #172535]: Compilation issue
>
> Question #172535 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/172535
>
> JM posted a new comment:
> Dear John,
> Its been a long while since I worked with the ARM. That was a
> Multitech developer platform. I will try to figure out what solution was it
> that I found back then. Meanwhile I posted on Multitech Developer platform
> at the time as well. Please go though these posts maybe they would be of
> help.
>
> http://www.multitech.net/developer/forums/topic.php?id=126
>
> Sorry as right now I do not exactly remember the solution.I hope to find it
> and get back to you soon. Take care and good luck.
>
> Regards,
>
> JM
>
>
> On Mon, May 27, 2013 at 6:36 PM, JohnMajor <
> <email address hidden>> wrote:
>
> > Your question #172535 on libmodbus changed:
> > https://answers.launchpad.net/libmodbus/+question/172535
> >
> > JohnMajor posted a new comment:
> > Hi JM
> >
> > I came across you post - I am having exactly the same problems with
> > compiling libmodbus (3.03) using eclipse for the arm?
> >
> > You mentioned that the problem was that the arm cross compiler was not
> > compiling against the libmodbus library - but never posted you solution
> on
> > how to solve this problem?
> > Any help would be much appreciated
> > regards
> > John
> >
> > --
> > You received this question notification because you asked the question.
> >
>
> --
> You received this question notification because you are a direct
> subscriber of the question.
>
> You received this question notification because you asked the question.
>

Revision history for this message
Dmitriy (dmitriyplestsov) said :
#18

Hello guys,

 I'm Eclipse Kepler + libmodbus-3.0.4 + gcc x86 user as well and I have problem with test modbus project compilng. I have got
                              "undefined reference to `modbus_new_tcp' "
                             " undefined reference to `modbus_connect' "
                             " undefined reference to `modbus_read_registers' "
                              "undefined reference to `modbus_close' "
                              "undefined reference to `modbus_free' "
linker errors.

Here source:

#include <modbus/modbus-rtu.h>
#include <modbus/modbus.h>
#include "unit-test.h"

int main(void)
{
 modbus_t *mb;
  uint16_t tab_reg[32];

  mb = modbus_new_tcp("127.0.0.1", 1502);
  modbus_connect(mb);

  /* Read 5 registers from the address 0 */
  modbus_read_registers(mb, 0, 5, tab_reg);

  modbus_close(mb);
  modbus_free(mb);

  while(1);
}

functions headers are presents in modbus.h

 libmodbus has installed succed, I tried to compile an example from Tests folder by command line. Executable file has been generated.

 What the problem may be please ?

Thank you in advance