Closed kezl closed 9 years ago
Your malloc call is unnecessary. Having declared struct sp_port **ports
, you just pass &ports
to sp_list_ports()
and it will set ports
to point to the array it allocates.
This version of your code runs fine for me:
/* gcc test2.c -lserialport -o test2 */
#include <stdio.h>
#include <libserialport.h>
int main() {
struct sp_port **ports = NULL;
struct sp_port *port = NULL;
int result = SP_OK;
result = sp_list_ports(&ports);
if(result == SP_OK ) {
printf("Operation completed successfully. \n");
} else if(result == SP_ERR_ARG) {
printf("Invalid arguments were passed to the function.\n");
} else if(result == SP_ERR_FAIL) {
printf("A system error occured while executing the operation.");
} else if(result == SP_ERR_MEM ) {
printf("A memory allocation failed while executing the operation.");
} else if(result == SP_ERR_SUPP) {
printf("The requested operation is not supported by this system or device. ");
}
int i = 0;
while (ports[i] != NULL) {
port = ports[i];
const char* name = sp_get_port_name(port);
printf("%s\n", name);
i++;
}
sp_free_port_list(ports);
return 0;
}
On 16/03/15 10:14, Martin Ling wrote:
Closed #7 https://github.com/martinling/libserialport/issues/7.
— Reply to this email directly or view it on GitHub https://github.com/martinling/libserialport/issues/7#event-255290991.
Still no joy for me, using your example after a clean install I can build but I get the following error when the program is run: ./test2: error while loading shared libraries: libserialport.so.0: cannot open shared object file: No such file or directory
It seems odd that it looks for a shared library.
When I tried building with with libserialport.a in the same directory: gcc -I/path/to/source/libserialport/ -o test3 test3.c ./libserialport.a it worked as expected so I will do it that way.
Best regards, kezl
The way you have been building using files directly from the libserialport tree is very odd.
The normal way to use the library would be to run 'make install' in the libserialport source directory, which will install the headers and library in /usr/local.
You can then build your own program elsewhere, including <libserialport.h>
and building with -lserialport
. The result will be a dynamically linked executable which finds and uses the installed copy of the library when it runs.
If you want a static executable, i.e. one that does not depend on the presence of a shared library, then just build with the -static
flag and the necessary parts of the library will be incorporated directly into your executable.
The libserialport.so.0 error comes after I run make install using the usual method as per the readme and then build the example you gave, so building against the installed copy doesn't seem to work for me.
If I build a static executable from components in the same directory it works , so I am starting to think maybe something is corrupt on my path so I will look there, thankyou for your help.
You may just need to run sudo ldconfig
to update things after having installed the library.
On 16/03/15 12:55, Martin Ling wrote:
|sudo ldconfig| Yes, that fixed it :) thank-you again!
Hello,
Using the code below I am getting a segfault error and I am starting to think my allocation is wrong somehow but I can't see where, am I missing something? My operating system is debian jessie and I am using a copy of the library built on the same machine. I have tried calling the resultant executable as normal and root user and get the same error so I don't think it is permissions related.
Best regards, kezl