libmodbus: how to compile application including libmodbus library? - c

I have a problem with compilation of my application, which is based on libmodbus library:
https://github.com/stephane/libmodbus
I have tried to compile it using this command:
cc pkg-config --cflags --libs libmodbus modbus.c -o modbus
But I get the following errors:
/usr/bin/ld: /tmp/cckdtbWy.o: in function `main':
modbus.c:(.text+0x1b): undefined reference to `modbus_new_tcp_pi'
/usr/bin/ld: modbus.c:(.text+0x5c): undefined reference to `modbus_connect'
/usr/bin/ld: modbus.c:(.text+0x8d): undefined reference to `modbus_free'
/usr/bin/ld: modbus.c:(.text+0xa3): undefined reference to `modbus_connect'
/usr/bin/ld: modbus.c:(.text+0xde): undefined reference to `modbus_read_bits'
/usr/bin/ld: modbus.c:(.text+0x11e): undefined reference to `modbus_close'
/usr/bin/ld: modbus.c:(.text+0x12a): undefined reference to `modbus_free'
collect2: error: ld returned 1 exit status
I would appreciate any help!
Best regards!

I have worked through this problem and my solution is that:
1. Create modbus.conf file in /etc/ld.so.conf.d/.
2. Run sudo ldconfig.
3. Compile your application/program with the following command:
gcc <name>.c -o <name> -I/usr/local/include/modbus/ -lmodbus
It has worked for me, but I am not sure whether the first step is necessary.

Did you install the libmodbus-dev? You should try this: sudo apt-get install -y libmodbus-dev.
See v3.1.6 and v3.0.8: libmodbus docs

Related

mcsema using error,has someone use mcsema successful

I flow the Official website to build mcsema using native build approach.Everything seems OK.Since my ida is on Windows,so I use the command of
%IDA_ROOT%\ida64.exe -S"C:\Users\dkl\Desktop\source\mcsema\tools\mcsema_disass\ida7\get_cfg.py --output
C:\Users\dkl\Desktop\output.cfg --arch amd64 --os linux" C:\Users\dkl\Desktop\work\t2\a.out
to generate output.cfg ,that is OK
then
mcsema-lift-11.0 --arch amd64 --os linux --cfg ./output.cfg --output ./output.bc
that is OK,then
remill-clang-11 -o ./output ./output.bc -lpthread -lm -ldl -llzma -Wl,--section-start=.section_1ff00000=0x1ff00000
COUNTER A ERROR
root#DESKTOP-CS6BMJI:/mnt/c/Users/dkl/Desktop# mcsema-lift-11.0 --arch amd64 --os linux --cfg ./output.cfg --output ./output.bc
root#DESKTOP-CS6BMJI:/mnt/c/Users/dkl/Desktop# remill-clang-11 -o ./output ./output.bc -lpthread -lm -ldl -llzma -Wl,--section-start=.section_1ff00000=0x1ff00000
warning: overriding the module target triple with x86_64-unknown-linux-gnu [-Woverride-module]
1 warning generated.
/usr/bin/ld: /tmp/output-3e1099.o: in function sub_401020': llvm-link:(.text+0x152): undefined reference to __remill_jump'
/usr/bin/ld: /tmp/output-3e1099.o: in function sub_401040__start': llvm-link:(.text+0x2cb): undefined reference to __remill_function_call'
/usr/bin/ld: /tmp/output-3e1099.o: in function sub_401160___libc_csu_init': llvm-link:(.text+0x939): undefined reference to __remill_function_call'
/usr/bin/ld: /tmp/output-3e1099.o: in function sub_401000__init_proc': llvm-link:(.text+0xe2d): undefined reference to __remill_function_call'
/usr/bin/ld: /tmp/output-3e1099.o: in function ext_404040_printf': llvm-link:(.text+0x10da): undefined reference to __remill_function_call'
/usr/bin/ld: /tmp/output-3e1099.o:(.rodata+0x2138): undefined reference to `__mcsema_attach_call'
remill-clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
root#DESKTOP-CS6BMJI:/mnt/c/Users/dkl/Desktop#

understanding compilation commands C and configuring CodeBlocks

I'm learning C in college. And I wanted to try new things, so I decided to use the allegro Game library to create some stuff. I followed the tutorial, and everything went fine. I wrote a piece of code in the text editor and executed the commands provided by the tutorial and it compiled and run. (I'm on linux btw).
These are the commands:
gcc hello.c -o hello $(pkg-config allegro-5 allegro_font-5 --libs --cflags)
./hello
So, I understand that gcc is calling the gcc compiler and hello.c is the name of the source code file and -o hello specifies the name of the compiled file. but the next part is ambiguous and beyond my knowledge:
$(pkg-config allegro-5 allegro_font-5 --libs --cflags)
So can anyone explain what it means (it has something to do with linking). and if I remove that part the compiler returns these error codes:
/usr/bin/ld: /tmp/ccIQsEis.o: in function `main':
hello.c:(.text+0x2f): undefined reference to `al_install_system'
/usr/bin/ld: hello.c:(.text+0x34): undefined reference to `al_install_keyboard'
/usr/bin/ld: hello.c:(.text+0x45): undefined reference to `al_create_timer'
/usr/bin/ld: hello.c:(.text+0x4e): undefined reference to `al_create_event_queue'
/usr/bin/ld: hello.c:(.text+0x61): undefined reference to `al_create_display'
/usr/bin/ld: hello.c:(.text+0x6a): undefined reference to `al_create_builtin_font'
/usr/bin/ld: hello.c:(.text+0x73): undefined reference to `al_get_keyboard_event_source'
/usr/bin/ld: hello.c:(.text+0x85): undefined reference to `al_register_event_source'
/usr/bin/ld: hello.c:(.text+0x91): undefined reference to `al_get_display_event_source'
/usr/bin/ld: hello.c:(.text+0xa3): undefined reference to `al_register_event_source'
/usr/bin/ld: hello.c:(.text+0xaf): undefined reference to `al_get_timer_event_source'
/usr/bin/ld: hello.c:(.text+0xc1): undefined reference to `al_register_event_source'
/usr/bin/ld: hello.c:(.text+0xd4): undefined reference to `al_start_timer'
/usr/bin/ld: hello.c:(.text+0xe7): undefined reference to `al_wait_for_event'
/usr/bin/ld: hello.c:(.text+0x125): undefined reference to `al_is_event_queue_empty'
/usr/bin/ld: hello.c:(.text+0x13d): undefined reference to `al_map_rgb'
/usr/bin/ld: hello.c:(.text+0x16a): undefined reference to `al_clear_to_color'
/usr/bin/ld: hello.c:(.text+0x17e): undefined reference to `al_map_rgb'
/usr/bin/ld: hello.c:(.text+0x1ca): undefined reference to `al_draw_text'
/usr/bin/ld: hello.c:(.text+0x1cf): undefined reference to `al_flip_display'
/usr/bin/ld: hello.c:(.text+0x1e7): undefined reference to `al_destroy_font'
/usr/bin/ld: hello.c:(.text+0x1f3): undefined reference to `al_destroy_display'
/usr/bin/ld: hello.c:(.text+0x1ff): undefined reference to `al_destroy_timer'
/usr/bin/ld: hello.c:(.text+0x20b): undefined reference to `al_destroy_event_queue'
collect2: error: ld returned 1 exit status
And for the second part of the question, I tried to compile this code in code:blocks but it returns the same errors given by the compiler without the term: $(pkg-config allegro-5 allegro_font-5 --libs --cflags)
So, what configuration should I change in codeblocks so that it compiles well code using allegro libraries.
PS. I didn't include the source code intentionally as it adds no useful information to the question, only clutters it.
This is a Bash command-syntax feature which executes another command and then provides its output as a string. This idiom is being used here to programmatically provide command-line parameters to the gcc command. The pkg-config command is being executed, and it returns a string which becomes part of – is textually included in – the command-string for invoking gcc. (Which of course neither knows nor cares where its command-line parameters "come from.")
(Clever, huh?)
For example, try this:
echo $(ls)
You will be rewarded with a list of all the files in the current directory, concatenated as a string.
To elaborate on the answer by #Mike Robinson,
$(pkg-config allegro-5 allegro_font-5 --libs --cflags) will execute the command pkg-config and substitute the result. pkg-config is build utility that helps when compiling C programs. It's not specific to allegro, it is used by many C libraries.
In this particular case, pkg-config is called with the parameters allegro-5 allegro_font-5 --libs --cflags, which asks pkg-config "what are the compiler options (--cflags) and libraries (--libs) I need when compiling and linking with allegro?" Allegro consists of several modules, in this case you chose the main module 'allegro' and the font module 'allegro_font'. In both cases you're referring to major version 5 of allegro.
On your system, the result of the command is -I/usr/include/x86_64-linux-gnu -lallegro_font -lallegro. The meaning of these options is as follows:
-I/usr/include/x86_64-linux-gnu tells the compiler to add the path /usr/include/x86_64-linux-gnu to the header search path (because the allegro headers are located there)
-lallegro_font and '-lallegro` tells the linker to add the allegro_font and allegro libraries respectively. That also explains why you get all those undefined reference errors when you remove this - you are no longer telling the linker to add the allegro library, so it can't find all those functions anymore.

Word Embedding Compression by Binarization (Tissier) // C-Binaries

For my bachelor thesis I want to apply this algorithm proposed here: Binarization by Tissier
I cloned the repository and tried to run cd near-lossless-binarization in the corrsponding directory.
However there comes up an error I don't know how to solve.
cd near-lossless-binarization && make
gcc binarize.c -o binarize -ansi -pedantic -Wall -Wextra -Wno-unused-result -Ofast -funroll-loops -lblas -lm
/usr/bin/ld: /tmp/ccxe5d0J.o: in function `apply_regularizarion_gradient':
binarize.c:(.text+0xddc): undefined reference to `cblas_sgemm'
/usr/bin/ld: binarize.c:(.text+0xf9e): undefined reference to `cblas_sgemm'
/usr/bin/ld: /tmp/ccxe5d0J.o: in function `apply_reconstruction_gradient':
binarize.c:(.text+0x1059): undefined reference to `cblas_sgemm'
/usr/bin/ld: binarize.c:(.text+0x12fb): undefined reference to `cblas_sgemm'
/usr/bin/ld: binarize.c:(.text+0x1800): undefined reference to `cblas_sgemm'
/usr/bin/ld: /tmp/ccxe5d0J.o:binarize.c:(.text+0x1e71): more undefined references to `cblas_sgemm' follow
collect2: Error: ld gab 1 as end-Status returned
Seems like there is an issue inside the corresponding file.
I already opened an issue in the repo, but need answers pretty soon..
Hoped to find anyone with C experience here who may solve my problem.
Requirements named in the repo are fulfilled.

MonetDB client compilation

I'm a beginner in the Database community. I have been trying to interface with MonetDB using C/C++ MAPI. I have installed MonetDB on my Ubuntu 14.04 LTS machine and when I try to compile a sample program from here, I get the following error:
$ gcc test.c -I /usr/include/monetdb -lmapi
test.c:(.text+0x29): undefined reference to mapi_explain_query'
test.c:(.text+0x35): undefined reference tomapi_result_error'
test.c:(.text+0x50): undefined reference to mapi_explain_result'
test.c:(.text+0x5c): undefined reference tomapi_next_result'
test.c:(.text+0x6d): undefined reference to mapi_close_handle'
test.c:(.text+0x79): undefined reference tomapi_destroy'
test.c:(.text+0x98): undefined reference to mapi_explain'
test.c:(.text+0xa4): undefined reference tomapi_destroy'
/tmp/cctyQopc.o: In function query':
test.c:(.text+0xf9): undefined reference tomapi_query'
test.c:(.text+0x110): undefined reference to mapi_error'
/tmp/cctyQopc.o: In functionupdate':
test.c:(.text+0x160): undefined reference to mapi_close_handle'
/tmp/cctyQopc.o: In functionmain':
test.c:(.text+0x1b6): undefined reference to mapi_connect'
test.c:(.text+0x1c6): undefined reference tomapi_error'
test.c:(.text+0x238): undefined reference to mapi_fetch_field'
test.c:(.text+0x24d): undefined reference tomapi_fetch_field'
test.c:(.text+0x277): undefined reference to mapi_fetch_row'
test.c:(.text+0x287): undefined reference tomapi_close_handle'
test.c:(.text+0x293): undefined reference to `mapi_destroy'
collect2: error: ld returned 1 exit status
This question has previously been asked here and the answer did not solve the problem (although the answer was accepted, I'm not sure why). Any help/directions would be great!
On Ubuntu 18.04 using:
pkg-config --libs monetdb-mapi and pkg-config --cflags monetdb-mapi
Won't work because Ubuntu doesn't have a bzip2.pc:
Here how it worked:
gcc main.c -I /usr/include/monetdb -lmapi -lssl -lcrypto -lstream -lcurl -llzma -lbz2

Compiling C code with Redis on linux

How to correct following error when I compiled test code written in C using credis API:
gcc -g -Wall -lcredis test_redis.c -o redisOUT
/tmp/ccetckOb.o: In function `main':
/home/faris/Downloads/credis-0.2.3/test_redis.c:12: undefined reference to `credis_connect'
/home/faris/Downloads/credis-0.2.3/test_redis.c:15: undefined reference to `credis_ping'
/home/faris/Downloads/credis-0.2.3/test_redis.c:18: undefined reference to `credis_set'
/home/faris/Downloads/credis-0.2.3/test_redis.c:21: undefined reference to `credis_get'
/home/faris/Downloads/credis-0.2.3/test_redis.c:25: undefined reference to `credis_close'
collect2: error: ld returned 1 exit status
Example on: http://code.google.com/p/credis/wiki/Examples
I'm busting my head for so long.
Thanks for solution.
Sam Iverish
Put the library after the source file that references it:
gcc -g -Wall test_redis.c -lcredis -o redisOUT

Resources