Linker issues with msys2 mingw64 package library (libserialport) in Codelite - undefined reference

89 Views Asked by At

I am running some very simple experiments with the libserialport library installed through msys2 however, I run into linking problems no matter what configuration I set in the project settings. I am compiling with the latest version of gcc 13.2 from msys2.

linking issues

I have confirmed that the library files are in the correct directories:

C:\msys64\mingw64\lib : libserialport.a

C:\msys64\mingw64\include: libserialport.h

C:\msys64\mingw64\bin: libserialport-0.dll

I surely might be missing something obvious for more experienced ones.

[Note] I am aware of this post, however, changing the Makefile generator to just "Make" or Codelite's does not work (compilation gets stuck. Maybe I am doing something really wrong :/ )

:beers:

As I mentioned, I have tried multiple combinations of settings (these aren't the only ones I tried. I have pointed at the dll, made the linking -static, etc.):

compiler settings linker settings

Additionally, my environment variables point at the right folders from the mingw64 location (hence why other projects work fine)

Please note that my Codelite works flawlessly with other projects. Additionally, my msys2 system is up to date and other contributed libraries work well (not all of them tested but no linking problems so far).

In case it works, I am testing with the list_ports.c example from their wiki page:

#include <libserialport.h>
#include <stdio.h>
/* Example of how to get a list of serial ports on the system.
 *
 * This example file is released to the public domain. */
int main(int argc, char **argv)
{
        /* A pointer to a null-terminated array of pointers to
         * struct sp_port, which will contain the ports found.*/
        struct sp_port **port_list;
        printf("Getting port list.\n");
        /* Call sp_list_ports() to get the ports. The port_list
         * pointer will be updated to refer to the array created. */
        enum sp_return result = sp_list_ports(&port_list);
        if (result != SP_OK) {
                printf("sp_list_ports() failed!\n");
                return -1;
        }
        /* Iterate through the ports. When port_list[i] is NULL
         * this indicates the end of the list. */
        int i;
        for (i = 0; port_list[i] != NULL; i++) {
                struct sp_port *port = port_list[i];
                /* Get the name of the port. */
                char *port_name = sp_get_port_name(port);
                printf("Found port: %s\n", port_name);
        }
        printf("Found %d ports.\n", i);
        printf("Freeing port list.\n");
        /* Free the array created by sp_list_ports(). */
        sp_free_port_list(port_list);
        /* Note that this will also free all the sp_port structures
         * it points to. If you want to keep one of them (e.g. to
         * use that port in the rest of your program), take a copy
         * of it first using sp_copy_port(). */
        return 0;
}
1

There are 1 best solutions below

0
On

Thanks to @stark for finding the right solution for me. I simply had to include the library in the linker. Dumb me.

As I mentioned in my last comment, I noticed there apparently was a bug in CodeLite, however, I can't replicate it anymore ¬¬ (Murphy's Law?)

The non-repeatable bug consisted of the following:

Despite adding the reference to the library in the linker options: -llibserialport, the linking stage would still fail. Changing the Makefile Generator setting from CodeLite Makefile Generator to Default magically made my errors disappear and the program built and ran just fine.

successful execution

Then, reverting the setting back to CodeLite Makefile Generator and after cleaning the project the code built and ran again without any problems.

From now on, I believe that I will select the Default Generator for all my projects as it seems to work from the get-go.