You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Discord Game SDK编译时遇未定义引用错误求助

Discord Game SDK v3.2.1 未定义引用链接错误解决

问题背景

在修改Discord Game SDK v3.2.1示例代码实现Rich Presence功能时,遇到链接阶段的未定义引用错误,SDK资源来自Discord开发者官网。

代码实现

#include <iostream>
#include <csignal>
#include <thread>
#include <chrono>
#include <string>

#include "../include/discord/discord.h"

struct DiscordState {
    std::unique_ptr<discord::Core> core;
};

namespace {
    volatile bool interrupted{ false };
}

int main(int argc, char* argv[]) {

    // Make sure we have the right number of arguments
    if (argc < 2 || argc > 2) {
        fprintf(stderr, "Incorrect number of arguments: expected 1, received %d.\n", (argc - 1));
        fprintf(stderr, "USAGE: ./StremioRichPresence [CLIENT_ID]\n");
        exit(1);
    }

    printf("StremioRichPresence\n");

    // Check if the argument represents an actual integer for a client id, otherwise complain.
    signed long int clientID = NULL;
    try {
        clientID = std::stoi(argv[1]);
    } catch (const std::invalid_argument& e) {
        fprintf(stderr, "Argument for client ID is not a valid number.\n");
        exit(1);
    } catch (const std::out_of_range& e) {
        fprintf(stderr, "Argument for client ID is a number too large to fit into the specified integer type.\n");
        exit(1);
    }
    printf("clientID is %ld\n", clientID);


    DiscordState state;
    discord::Core* core{};
    auto response = discord::Core::Create(clientID, DiscordCreateFlags_Default, &core);
    state.core.reset(core);

    if (!state.core) {
        std::cout << "Failed to instantiate Discord!";
        std::exit(-1);
    }

    discord::Activity activity{};
    activity.SetDetails("Fruit Tarts");
    activity.SetState("Pop Snacks");
    activity.GetAssets().SetSmallImage("the");
    activity.GetAssets().SetSmallText("i mage");
    activity.GetAssets().SetLargeImage("the");
    activity.GetAssets().SetLargeText("u mage");
    activity.SetType(discord::ActivityType::Playing);
    state.core->ActivityManager().UpdateActivity(activity, [](discord::Result result) {
        std::cout << ((result == discord::Result::Ok) ? "Succeeded" : "Failed")
            << " updating activity!\n";
    });


    std::signal(SIGINT, [](int) {
        interrupted = true;
    });
    do {
        state.core->RunCallbacks();
        std::this_thread::sleep_for(std::chrono::milliseconds(16));
    } while (!interrupted);

    return 0;
}

使用的编译命令

g++ -L/home/judith/Code/StremioRichPresence/lib/x86_64/ -l:discord_game_sdk.so -I/home/judith/Code/StremioRichPresence/include/discord src/main.cpp -o main.o

编译错误信息

/usr/bin/ld: /tmp/ccMXqSZb.o: in function `main':
main.cpp:(.text+0x1fe): undefined reference to `discord::Core::Create(long, unsigned long, discord::Core**)'
/usr/bin/ld: main.cpp:(.text+0x286): undefined reference to `discord::Activity::SetDetails(char const*)'
/usr/bin/ld: main.cpp:(.text+0x29f): undefined reference to `discord::Activity::SetState(char const*)'
/usr/bin/ld: main.cpp:(.text+0x2ae): undefined reference to `discord::Activity::GetAssets()'
/usr/bin/ld: main.cpp:(.text+0x2c3): undefined reference to `discord::ActivityAssets::SetSmallImage(char const*)'
/usr/bin/ld: main.cpp:(.text+0x2d2): undefined reference to `discord::Activity::GetAssets()'
/usr/bin/ld: main.cpp:(.text+0x2e7): undefined reference to `discord::ActivityAssets::SetSmallText(char const*)'
/usr/bin/ld: main.cpp:(.text+0x2f6): undefined reference to `discord::Activity::GetAssets()'
/usr/bin/ld: main.cpp:(.text+0x30b): undefined reference to `discord::ActivityAssets::SetLargeImage(char const*)'
/usr/bin/ld: main.cpp:(.text+0x31a): undefined reference to `discord::Activity::GetAssets()'
/usr/bin/ld: main.cpp:(.text+0x32f): undefined reference to `discord::ActivityAssets::SetLargeText(char const*)'
/usr/bin/ld: main.cpp:(.text+0x343): undefined reference to `discord::Activity::SetType(discord::ActivityType)'
/usr/bin/ld: main.cpp:(.text+0x35a): undefined reference to `discord::Core::ActivityManager()'
/usr/bin/ld: main.cpp:(.text+0x38f): undefined reference to `discord::ActivityManager::UpdateActivity(discord::Activity const&, std::function<void (discord::Result)>)'
/usr/bin/ld: main.cpp:(.text+0x3d1): undefined reference to `discord::Core::RunCallbacks()'
/usr/bin/ld: /tmp/ccMXqSZb.o: in function `std::default_delete<discord::Core>::operator()(discord::Core*) const':
main.cpp:(.text._ZNKSt14default_deleteIN7discord4CoreEEclEPS1_[_ZNKSt14default_deleteIN7discord4CoreEEclEPS1_]+0x1e): undefined reference to `discord::Core::~Core()'
collect2: error: ld returned 1 exit status

项目结构

.
├── build
├── include
│   └── discord
│       ├── achievement_manager.cpp
│       ├── achievement_manager.h
│       ├── activity_manager.cpp
│       ├── activity_manager.h
│       ├── application_manager.cpp
│       ├── application_manager.h
│       ├── core.cpp
│       ├── core.h
│       ├── discord.h
│       ├── event.h
│       ├── ffi.h
│       ├── image_manager.cpp
│       ├── image_manager.h
│       ├── lobby_manager.cpp
│       ├── lobby_manager.h
│       ├── network_manager.cpp
│       ├── network_manager.h
│       ├── overlay_manager.cpp
│       ├── overlay_manager.h
│       ├── relationship_manager.cpp
│       ├── relationship_manager.h
│       ├── storage_manager.cpp
│       ├── storage_manager.h
│       ├── store_manager.cpp
│       ├── store_manager.h
│       ├── types.cpp
│       ├── types.h
│       ├── user_manager.cpp
│       ├── user_manager.h
│       ├── voice_manager.cpp
│       └── voice_manager.h
├── lib
│   ├── aarch64
│   │   ├── discord_game_sdk.bundle
│   │   └── discord_game_sdk.dylib
│   └── x86_64
│       ├── discord_game_sdk.bundle
│       ├── discord_game_sdk.dll
│       ├── discord_game_sdk.dll.lib
│       ├── discord_game_sdk.dylib
│       └── discord_game_sdk.so
├── README.md
└── src
    └── main.cpp

解决方案

1. 调整编译命令参数顺序

GCC链接器按传入顺序处理文件和库,必须把源文件放在库参数之前,否则链接器无法匹配依赖符号。修改后的命令:

g++ src/main.cpp -I/home/judith/Code/StremioRichPresence/include -L/home/judith/Code/StremioRichPresence/lib/x86_64/ -ldiscord_game_sdk -o main
  • -I 指定头文件根目录,代码中可改为 #include <discord/discord.h> 更规范
  • 源文件 src/main.cpp 放在最前面,再依次指定头文件路径、库路径和库名

2. 编译SDK的C++绑定代码

include/discord 下的 .cpp 文件是Discord SDK的C++封装层,需要一起编译到项目中,否则头文件声明的函数没有对应实现。更新命令:

g++ src/main.cpp include/discord/*.cpp -I/home/judith/Code/StremioRichPresence/include -L/home/judith/Code/StremioRichPresence/lib/x86_64/ -ldiscord_game_sdk -o main

3. 验证库兼容性

  • 确认 lib/x86_64/discord_game_sdk.so 是Linux x86_64架构的编译版本,与当前系统匹配
  • 检查头文件和库文件版本一致,均为Discord Game SDK v3.2.1,避免版本不匹配导致符号差异

4. 设置运行时库路径

编译完成后,运行程序需让系统找到SDK库,可通过以下方式:

  • 将 lib/x86_64/discord_game_sdk.so 复制到 /usr/lib 或 /usr/local/lib
  • 运行前设置环境变量:export LD_LIBRARY_PATH=/home/judith/Code/StremioRichPresence/lib/x86_64:$LD_LIBRARY_PATH

内容的提问来源于stack exchange,提问作者Judith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 13:54:56