问题
I am a newbie in C... I wrote a very simple modbus1.c that includes libmodbus (whose source I downloaded, unzipped, untarred, ./configure'd, make'd and make install'd successfully).
When I try to make modbus1.c I get this:
cc -Wall -g modbus1.c -o modbus1
Undefined symbols for architecture x86_64:
"_modbus_close", referenced from:
_main in modbus1-6cd135.o
"_modbus_connect", referenced from:
_main in modbus1-6cd135.o
"_modbus_free", referenced from:
_main in modbus1-6cd135.o
"_modbus_new_tcp_pi", referenced from:
_main in modbus1-6cd135.o
"_modbus_read_bits", referenced from:
_main in modbus1-6cd135.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [modbus1] Error 1
I am running OSX snow leopard and have successfully used make to compile small programs before (tutorial level programs...) Here is the modbus1.c I am trying to compile:
#include <stdio.h>
#include <stdlib.h>
#include <modbus.h>
int main(int argc, char *argv[]){
modbus_t *plc_client;
plc_client = modbus_new_tcp_pi("192.168.1.230","502");
if (plc_client == NULL) {
fprintf(stderr, "Unable to allocate libmodbus context\n");
return -1;
}
if (modbus_connect(plc_client) == -1) {
fprintf(stderr, "Connection failed: \n");
modbus_free(plc_client);
return -1;
}
else if(modbus_connect(plc_client) == 0) {
printf("MODBUS CONNECTION SUCCESSFUL\n");
}
uint8_t* catcher = malloc(sizeof(uint8_t));
if(modbus_read_bits(plc_client, 2000, 1, catcher)>0){
printf("READ SUCCESSFUL");
}
else{
printf("READ FAILED");
}
free(catcher);
modbus_close(plc_client);
modbus_free(plc_client);
return 0;
}
Any help will be greatly appreciated! Thanks!
-Niko
回答1:
Try this
cc -Wall -g modbus1.c -o modbus1 -L/path/to/libmodbus -lmodbus
You should replace that /path/to/libmodbus
with the actual path of directory that includes the libmodbus.dylib
in your system.
来源:https://stackoverflow.com/questions/22697543/compiling-a-program-that-includes-libmodbus-in-c