(C++) glibmm 不会在 Ubuntu/Oneiric 上链接

发布于 2024-12-28 11:48:37 字数 978 浏览 3 评论 0原文

我在 Ubuntu/Oneiric 上运行最简单的程序时遇到问题:

#include <glibmm/ustring.h>

int main()
{
    Glib::ustring s = "Test string";
}

在 make 上使用 Makefile

PACKAGES=glibmm-2.4 glib-2.0 gtkmm-3.0 gtk+-3.0
CC=g++
CFLAGS=`pkg-config --cflags $(PACKAGES)` --std=c++0x
LD=g++
LDFLAGS=`pkg-config --libs $(PACKAGES)`

build: ./main

run: build
    ./main

clean:
    rm ./main.o

rebuild: clean build

./main: ./main.o
    $(LD) $(LDFLAGS) ./main.o -o ./main

./main.o: ./main.cc
    $(CC) $(CFLAGS) ./main.cc -c -o ./main.o

会出现以下错误:

./main.o: In function `main':
main.cc:(.text+0x15): undefined reference to `Glib::ustring::ustring(char const*)'
main.cc:(.text+0x21): undefined reference to `Glib::ustring::~ustring()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

在 Ubuntu/Maverick 上,完全相同的代码与完全相同的文件链接良好... 如果在 main.o 上使用 ld,它也可以成功链接,但是(正如预期的那样)缺少 _start...

有什么建议吗?

I have problems clinking simplest program on Ubuntu/Oneiric:

#include <glibmm/ustring.h>

int main()
{
    Glib::ustring s = "Test string";
}

using Makefile

PACKAGES=glibmm-2.4 glib-2.0 gtkmm-3.0 gtk+-3.0
CC=g++
CFLAGS=`pkg-config --cflags $(PACKAGES)` --std=c++0x
LD=g++
LDFLAGS=`pkg-config --libs $(PACKAGES)`

build: ./main

run: build
    ./main

clean:
    rm ./main.o

rebuild: clean build

./main: ./main.o
    $(LD) $(LDFLAGS) ./main.o -o ./main

./main.o: ./main.cc
    $(CC) $(CFLAGS) ./main.cc -c -o ./main.o

on make following errors appears:

./main.o: In function `main':
main.cc:(.text+0x15): undefined reference to `Glib::ustring::ustring(char const*)'
main.cc:(.text+0x21): undefined reference to `Glib::ustring::~ustring()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

On Ubuntu/Maverick the exactly same code links well with exactly same file...
if using ld on main.o it links successfully too but (as it was expected) _start is missing...

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2025-01-04 11:48:37

尝试将相关行更改为:

LDFLAGS=`pkg-config --libs-only-L --libs-only-other $(PACKAGES)`
LIBS=`pkg-config --libs-only-l $(PACKAGES)`

# ...

./main: ./main.o
    $(LD) $(LDFLAGS) ./main.o -o ./main $(LIBS)

原因是链接器可能会按照命令行上给出的顺序搜索库,因此应始终将它们放在最后以确保安全。

Try changing the relevant lines to this:

LDFLAGS=`pkg-config --libs-only-L --libs-only-other $(PACKAGES)`
LIBS=`pkg-config --libs-only-l $(PACKAGES)`

# ...

./main: ./main.o
    $(LD) $(LDFLAGS) ./main.o -o ./main $(LIBS)

The reason is that the linker may search libraries in the order they are given on the command line, so they should always be placed last to be safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文