我在Ubuntu上针对glib编译一个简单的示例程序时遇到了麻烦.我收到以下错误.我可以让它编译但不与-c标志链接,我相信这意味着我安装了glib标头,但它没有找到共享对象代码.另请参阅下面的Make文件.
$> make re
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0 re.c -o re
/tmp/ccxas1nI.o: In function `print_uppercase_words':
re.c:(.text+0x21): undefined reference to `g_regex_new'
re.c:(.text+0x41): undefined reference to `g_regex_match'
re.c:(.text+0x54): undefined reference to `g_match_info_fetch'
re.c:(.text+0x6e): undefined reference to `g_print'
re.c:(.text+0x7a): undefined reference to `g_free'
re.c:(.text+0x8b): undefined reference to `g_match_info_next'
re.c:(.text+0x97): undefined reference to `g_match_info_matches'
re.c:(.text+0xa7): undefined reference to `g_match_info_free'
re.c:(.text+0xb3): undefined reference to `g_regex_unref'
collect2: ld returned 1 exit status
make: *** [re] Error 1
Run Code Online (Sandbox Code Playgroud)
Makefile 用过的:
# Need to …Run Code Online (Sandbox Code Playgroud) 我有以下问题:
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG build/liblcthw.a tests/list_tests.c -o tests/list_tests
/tmp/ccpvGjZp.o: In function `test_create':
~/lcthw/tests/list_tests.c:12: undefined reference to `List_create'
collect2: ld returned 1 exit status
make: *** [tests/list_tests] Error 1
Run Code Online (Sandbox Code Playgroud)
但
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG tests/list_tests.c build/liblcthw.a -o tests/list_tests
Run Code Online (Sandbox Code Playgroud)
运行得很好,nm显示预期的内容,测试运行,每个人都很开心,等等.
我搜索了SO并找到了很多答案(例如链接器命令 - GCC),所以很明显链接器的工作原理应该如此.那么,我应该如何修改我的makefile以遵循命令呢?
到目前为止,这是Makefile:
CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
LIBS=$(OPTLIBS)
PREFIX?=/usr/local
BUILD=build
SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))
TARGET=$(BUILD)/liblcthw.a
TARGET_LINK=lcthw
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
#The …Run Code Online (Sandbox Code Playgroud) 我试图在PintOS makefile上运行make,但我一直得到未定义的引用"floor"错误.makefile在下面.我用gcc 4.6.1运行Ubuntu 11.10.任何帮助表示赞赏.
all: setitimer-helper squish-pty squish-unix
CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o
clean:
rm -f *.o setitimer-helper squish-pty squish-unix
Run Code Online (Sandbox Code Playgroud)
〜