c++ - header 未被识别

标签 c++ include makefile

背景

我有以下源码

#include <libubuntuone-1.0/u1-music-store.h>
#include <libsyncdaemon-1.0/libsyncdaemon/libsyncdaemon.h>

static void
get_credentials (U1MusicStore *music_store,
                                 gchar **oauth_consumer_token,
                                 gchar **oauth_consumer_secret,
                                 gchar **oauth_token,
                                 gchar **oauth_token_secret)
{
    SyncdaemonCredentials *credentials;
    *oauth_consumer_token = *oauth_consumer_secret = *oauth_token = *oauth_token_secret = NULL;

    *oauth_consumer_token = g_strdup (syncdaemon_credentials_get_consumer_key (credentials));
    *oauth_consumer_secret = g_strdup (syncdaemon_credentials_get_consumer_secret (credentials));
    *oauth_token = g_strdup (syncdaemon_credentials_get_token (credentials));
    *oauth_consumer_secret = g_strdup (syncdaemon_credentials_get_token_secret (credentials));
}

int main() 
{
    return 0;
}

我正在用下面的 makefile 编译它

main: main.o
    g++ main.o -o main

main.o: main.cpp
    g++ -c main.cpp `pkg-config --cflags --libs gtk+-2.0`

我需要包含 pkg-config 选项,因为 u1-music-store.h header 试图包含 gtk/gtk.h,但编译器无法自行找到它。

libsyncdaemon.h 是一个元 header ,其唯一目的是包含更大的 header 列表,如下所示

#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-authentication.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-config-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-credentials.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-daemon.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-events-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-file-info.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-filesystem-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-folder-info.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-folders-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-publicfiles-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-share-info.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-status-info.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-status-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-transfer-info.h>

我的问题

每当我尝试编译我的代码时,我都会收到以下错误:

main.o: In function `get_credentials(_U1MusicStore*, char**, char**, char**, char**)':
main.cpp:(.text+0x34): undefined reference to `syncdaemon_credentials_get_consumer_key'
main.cpp:(.text+0x3c): undefined reference to `g_strdup'
main.cpp:(.text+0x4e): undefined reference to `syncdaemon_credentials_get_consumer_secret'
main.cpp:(.text+0x56): undefined reference to `g_strdup'
main.cpp:(.text+0x68): undefined reference to `syncdaemon_credentials_get_token'
main.cpp:(.text+0x70): undefined reference to `g_strdup'
main.cpp:(.text+0x82): undefined reference to `syncdaemon_credentials_get_token_secret'
main.cpp:(.text+0x8a): undefined reference to `g_strdup'
collect2: ld returned 1 exit status
make: *** [main] Error

使用 grep,我已经追踪到 syncdaemon-credentials.h 的四个 syncdaemon_credentials_get_* 函数,我希望编译器能够找到,因为它列在 libsyncdaemon.h 中,但由于某种原因,这并没有发生。我假设这是因为 u1-music-store.h 无法找到 gtk/gtk.h 迫使我使用 pkg-我的 makefile 中的 config 选项,但我无法理解为什么一开始就是这种情况。如果文件的 #included,我希望它被编译器包含。

再次使用 grep 我能够追踪 g_strdup 到许多 header ,但我还发现当我用单个命令替换为 makefile 时

g++ main.cpp -o main `pkg-config --cflags --libs gtk+-2.0`

我可以消除 g_strdup 警告,只剩下函数错误。

我的问题

这里有两件事我想知道:

  1. 为了解决我的特定问题,我的 makefile 应该是什么样子
  2. 我的问题的一般解决方案是什么?我猜这与将 #include 指令菊花链在一起有关,并且必须使用 pkg-config 来解决这个问题,但我不确定。

最佳答案

  1. 您需要链接到 libsyncdaemon.so。从 pkg-config ... libsyncdaemon-1.0 获取适当的参数。

  2. 您需要链接导出所需符号的库。如果有 .pc 文件,那么您可以使用它来获取适当的参数。

关于c++ - header 未被识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6380082/

相关文章:

c++ - opencv 中 mpeg 解码器的 4 个字符 ID 是什么

reactjs - 使用 find 与 includes 有什么区别?

php mysqli_query 没有结果

linux - 构建 googletest 失败 ../allocators.h : No such file or directory (but it's present in/usr/local/include/)

c++ - GDB核心转储: Recover argc argv values after crash

c++ - 运行时检查失败 #2 S C++

c++ - OSX Mojave-ld:找不到架构x86_64铛的符号:错误:链接器命令失败,退出代码为1

c - fatal error : addFunc. h:没有那个文件或目录

c++ - 使用 makefile 中的隐式规则进行清理

c++ - 链接器错误 - clang++