【问题标题】:clang: error: linker command failed with exit code 1 (use -v to see invocation) MINIX3clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)MINIX3
【发布时间】:2016-02-25 04:02:04
【问题描述】:

我正在尝试在 MINIX3 上运行一个 C/C++ 应用程序,该应用程序应该使用 msgsnd() 和 msgget() 在两个进程之间使用 msg.h 发送消息。

这是我得到的错误:

send.cpp:(.text+0x7f): undefined reference to `msgget'
send.cpp:(.text+0x1c1): undefined reference to `msgsnd'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我正在使用clang++编译代码:

clang++ send.cpp -o send.out

这是 send.cpp 代码:

#include <lib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MSGSZ     128
/*
* Declare the message structure.
*/

typedef struct msgbufer {
    long    mtype;
    char    mtext[MSGSZ];
} message_buf;

int main()
{
    int msqid;
    int msgflg = IPC_CREAT | 0666;
    key_t key;
    message_buf sbuf;
    size_t buf_length;

    /*
    * Get the message queue id for the
    * "name" 1234, which was created by
    * the server.
    */
    key = 1234;

    (void)fprintf(stderr, "\nmsgget: Calling msgget(%i,\
                          %#o)\n",
                          key, msgflg);

    if ((msqid = msgget(key, msgflg)) < 0) {
        perror("msgget");
        exit(1);
    }
    else
        (void)fprintf(stderr, "msgget: msgget succeeded: msqid = %d\n", msqid);


    /*
    * We'll send message type 1
    */

    sbuf.mtype = 1;

    (void)fprintf(stderr, "msgget: msgget succeeded: msqid = %d\n", msqid);

    (void)strcpy(sbuf.mtext, "Hello other process 2.");

    (void)fprintf(stderr, "msgget: msgget succeeded: msqid = %d\n", msqid);

    buf_length = strlen(sbuf.mtext) + 1;



    /*
    * Send a message.
    */
    if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
        printf("%d, %li, %s, %lu\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
        perror("msgsnd");
        exit(1);
    }

    else
        printf("Message: \"%s\" Sent\n", sbuf.mtext);

    exit(0);
}

【问题讨论】:

  • 发布的代码缺少要发送的消息中mtype字段的设置。
  • 一般来说,lib.h 不存在(至少在 ubuntu linux 14.04 上)并且对发布的代码没有任何贡献。建议删除#include &lt;lib.h&gt; 声明。

标签: c++ c gcc clang minix


【解决方案1】:

您没有链接到包含 msgsndmsgget 函数的库,因此您的链接器步骤失败。我对 Minix 不熟悉,所以我不确定该库的存储位置或名称。基本上,您需要在链接步骤中添加-l&lt;msg&gt; 标志。其中&lt;msg&gt; 是包含实现的库的名称。

【讨论】:

  • 您能否编辑您的答案,以提供一个小示例的更多细节,说明我对 C/C++ 的经验很少。
  • 警告:我不使用clang。 clang 被吹捧为与 gcc 完全兼容。在 gcc 中,-L&lt;pathToLibraryies&gt;-l&lt;shortLibName&gt; 参数用于定位/选择库。错误消息表明库不在标准位置。因此,您需要使用-L-l 参数来启用链接步骤来查找所需的库。
猜你喜欢
  • 2016-02-12
  • 2016-04-15
  • 2013-05-15
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多