Codelite 中 msys2 mingw64 包库 (libserialport) 的链接器问题 - 未定义的引用

问题描述 投票:0回答:1

我正在使用通过 msys2 安装的 libserialport 库运行一些非常简单的实验,但是,无论我在项目设置中设置什么配置,我都会遇到链接问题。我正在使用 msys2 的最新版本的 gcc 13.2 进行编译。

我已确认库文件位于正确的目录中:

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

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

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

对于更有经验的人来说,我肯定可能会错过一些明显的东西。

[注意]我知道this帖子,但是,将Makefile生成器更改为“Make”或Codelite是行不通的(编译卡住了。也许我做错了什么:/)

:啤酒:

正如我提到的,我尝试了多种设置组合(这些不是我尝试的唯一组合。我已经指向 dll,进行了链接

-static
等):

此外,我的环境变量指向

mingw64
位置的正确文件夹(因此其他项目工作正常)

请注意,我的 Codelite 可以与其他项目完美配合。此外,我的 msys2 系统是最新的,其他贡献的库运行良好(并非所有库都经过测试,但到目前为止没有链接问题)。

如果它有效,我正在使用他们的 wiki 页面中的

list_ports.c
example 进行测试:

#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;
}
c gcc linker-errors msys2 codelite
1个回答
0
投票

感谢@stark为我找到了正确的解决方案。我只需要将库包含在链接器中即可。我傻了

正如我在上一篇评论中提到的,我注意到 CodeLite 中显然有一个 bug,但是,我无法再复制它 ØØ (墨菲定律?)

不可重复的bug包括以下内容:

尽管在链接器选项中添加了对库的引用:

-llibserialport
,链接阶段仍然会失败。将
Makefile Generator
设置从
CodeLite Makefile Generator
更改为
Default
神奇地 使我的错误消失,程序构建并运行得很好。

然后,将设置恢复为

CodeLite Makefile Generator
,清理项目后,代码构建并再次运行,没有任何问题。

从现在开始,我相信我会为我的所有项目选择

Default
生成器,因为它似乎从一开始就有效。

© www.soinside.com 2019 - 2024. All rights reserved.