'对 'function' 的未定义引用
'undefined reference to 'function'
我已经下载了库 libcomplearn
并想在一个小示例程序中测试它。但是当我 link 它时,我得到错误 undefined reference to ‘function’.
我将库安装到特定路径。
OS:Debian
test.c
#include <stdio.h>
#include "complearn.h"
#include "complearn/complearn-ncd.h"
int main(const int argc, const char * const argv[])
{
printf("Number from my library\n");
CompLearnNcd *ncd = complearn_ncd_top();
return 0;
}
生成文件
FILES = test
LIBPATH = /try/libcomplearn/lib/pkgconfig
OUTPUT = TK_1
LIBNAME = complearn
#--------------------------------------------------
CC = gcc
CFLAGS = -c -Wall `export PKG_CONFIG_PATH=$(LIBPATH) && pkg-config --cflags $(LIBNAME)`
LDFLAGS = -static `export PKG_CONFIG_PATH=$(LIBPATH) && pkg-config --libs --static $(LIBNAME) -llzma`
all: Release
Debug: CFLAGS += -g
Debug: $(OUTPUT)
Release: $(OUTPUT)
$(OUTPUT): $(OUTPUT).o
@echo "started...."
$(CC) -o $(OUTPUT) $(OUTPUT).o $(LDFLAGS)
@echo "done...."
$(OUTPUT).o: $(FILES).c
$(CC) $(CFLAGS) $(FILES).c -o $(OUTPUT).o
clean:
rm -f $(OUTPUT).o $(OUTPUT)
输出
gcc -c -Wall `export PKG_CONFIG_PATH=/try/libcomplearn/lib/pkgconfig &&
pkg-config --cflags complearn` test.c -o TK_1.o
test.c: In function ‘main’:
test.c:9:19: warning: unused variable ‘ncd’ [-Wunused-variable]
CompLearnNcd *ncd = complearn_ncd_top();
^~~
started....
gcc -o TK_1 TK_1.o -static `export
PKG_CONFIG_PATH=/try/libcomplearn/lib/pkgconfig && pkg-config --libs --
static complearn -llzma`
Unknown option -llzma
/usr/bin/ld: TK_1.o: in function `main':
test.c:(.text+0x1c): undefined reference to `complearn_ncd_top'
collect2: error: ld returned 1 exit status
make: *** [makefile:28: TK_1] Error 1
我也试过这个命令:
gcc test.c `-L/try/libcomplearn/lib/ -llzma` `pkg-config --cflags --libs glib-2.0`
来自Why does the order in which libraries are linked sometimes cause errors in GCC?
-l 选项的顺序非常重要。只需将 -llzma 作为最后一个链接选项传递即可。
你说:
I also tried with the command:
gcc test.c `-L/try/libcomplearn/lib/ -llzma` `pkg-config --cflags --libs glib-2.0`
表示你不明白back-ticks,
自:
`-L/try/libcomplearn/lib/ -llzma`
不是对它们的有意义的使用。现在花时间了解它们的用途。
你联动失败的原因是设置:
LDFLAGS = -static `export PKG_CONFIG_PATH=$(LIBPATH) && pkg-config --libs --static $(LIBNAME) -llzma`
在生成文件中。
这里你有 -llzma
在反引号扩展中:
`export PKG_CONFIG_PATH=$(LIBPATH) && \
pkg-config --libs --static $(LIBNAME) -llzma`
为了扩展它,shell 执行:
export PKG_CONFIG_PATH=$(LIBPATH)
pkg-config --libs --static $(LIBNAME) -llzma
-llzma
是 pkg-config
命令的无意义选项,因此失败,
如您所见,它在 make
输出中抱怨:
Unknown option -llzma
就像:
$ pkg-config --cflags --libs zlib -llzma
Unknown option -llzma
因此,需要输出的链接选项:
pkg-config --libs --static $(LIBNAME)
不输出,也不插入到 LDFLAGS
的值中。所以联动
失败:
test.c:(.text+0x1c): undefined reference to `complearn_ncd_top'
因为libcomplearn
还没有链接。更正 LDFLAGS
的设置
至:
LDFLAGS = -static `export PKG_CONFIG_PATH=$(LIBPATH) && pkg-config --libs --static $(LIBNAME)` -llzma
-llzma
在反引号扩展之后。
我已经下载了库 libcomplearn
并想在一个小示例程序中测试它。但是当我 link 它时,我得到错误 undefined reference to ‘function’.
我将库安装到特定路径。
OS:Debian
test.c
#include <stdio.h>
#include "complearn.h"
#include "complearn/complearn-ncd.h"
int main(const int argc, const char * const argv[])
{
printf("Number from my library\n");
CompLearnNcd *ncd = complearn_ncd_top();
return 0;
}
生成文件
FILES = test
LIBPATH = /try/libcomplearn/lib/pkgconfig
OUTPUT = TK_1
LIBNAME = complearn
#--------------------------------------------------
CC = gcc
CFLAGS = -c -Wall `export PKG_CONFIG_PATH=$(LIBPATH) && pkg-config --cflags $(LIBNAME)`
LDFLAGS = -static `export PKG_CONFIG_PATH=$(LIBPATH) && pkg-config --libs --static $(LIBNAME) -llzma`
all: Release
Debug: CFLAGS += -g
Debug: $(OUTPUT)
Release: $(OUTPUT)
$(OUTPUT): $(OUTPUT).o
@echo "started...."
$(CC) -o $(OUTPUT) $(OUTPUT).o $(LDFLAGS)
@echo "done...."
$(OUTPUT).o: $(FILES).c
$(CC) $(CFLAGS) $(FILES).c -o $(OUTPUT).o
clean:
rm -f $(OUTPUT).o $(OUTPUT)
输出
gcc -c -Wall `export PKG_CONFIG_PATH=/try/libcomplearn/lib/pkgconfig &&
pkg-config --cflags complearn` test.c -o TK_1.o
test.c: In function ‘main’:
test.c:9:19: warning: unused variable ‘ncd’ [-Wunused-variable]
CompLearnNcd *ncd = complearn_ncd_top();
^~~
started....
gcc -o TK_1 TK_1.o -static `export
PKG_CONFIG_PATH=/try/libcomplearn/lib/pkgconfig && pkg-config --libs --
static complearn -llzma`
Unknown option -llzma
/usr/bin/ld: TK_1.o: in function `main':
test.c:(.text+0x1c): undefined reference to `complearn_ncd_top'
collect2: error: ld returned 1 exit status
make: *** [makefile:28: TK_1] Error 1
我也试过这个命令:
gcc test.c `-L/try/libcomplearn/lib/ -llzma` `pkg-config --cflags --libs glib-2.0`
来自Why does the order in which libraries are linked sometimes cause errors in GCC?
-l 选项的顺序非常重要。只需将 -llzma 作为最后一个链接选项传递即可。
你说:
I also tried with the command:
gcc test.c `-L/try/libcomplearn/lib/ -llzma` `pkg-config --cflags --libs glib-2.0`
表示你不明白back-ticks, 自:
`-L/try/libcomplearn/lib/ -llzma`
不是对它们的有意义的使用。现在花时间了解它们的用途。
你联动失败的原因是设置:
LDFLAGS = -static `export PKG_CONFIG_PATH=$(LIBPATH) && pkg-config --libs --static $(LIBNAME) -llzma`
在生成文件中。
这里你有 -llzma
在反引号扩展中:
`export PKG_CONFIG_PATH=$(LIBPATH) && \
pkg-config --libs --static $(LIBNAME) -llzma`
为了扩展它,shell 执行:
export PKG_CONFIG_PATH=$(LIBPATH)
pkg-config --libs --static $(LIBNAME) -llzma
-llzma
是 pkg-config
命令的无意义选项,因此失败,
如您所见,它在 make
输出中抱怨:
Unknown option -llzma
就像:
$ pkg-config --cflags --libs zlib -llzma
Unknown option -llzma
因此,需要输出的链接选项:
pkg-config --libs --static $(LIBNAME)
不输出,也不插入到 LDFLAGS
的值中。所以联动
失败:
test.c:(.text+0x1c): undefined reference to `complearn_ncd_top'
因为libcomplearn
还没有链接。更正 LDFLAGS
的设置
至:
LDFLAGS = -static `export PKG_CONFIG_PATH=$(LIBPATH) && pkg-config --libs --static $(LIBNAME)` -llzma
-llzma
在反引号扩展之后。