c - 如何编译这个库以供使用?

标签 c gcc linker

我是 C 编程的新手,我正在尝试编译 this Simple training example在 Ubuntu 12.10 上使用 GCC。

看起来不应该包含 fann.h(如文件本身所述),所以我改为包含 fixedfann.h。

第一次尝试(没有包含,只是为了看看编译器会要求什么):

$ gcc main.c -o output 
/tmp/cckKyM92.o: In function `main':
main.c:(.text+0x62): undefined reference to `fann_create_standard'
main.c:(.text+0x7a): undefined reference to `fann_set_activation_function_hidden'
main.c:(.text+0x8e): undefined reference to `fann_set_activation_function_output'
main.c:(.text+0xba): undefined reference to `fann_train_on_file'
main.c:(.text+0xce): undefined reference to `fann_save'
main.c:(.text+0xda): undefined reference to `fann_destroy'
collect2: ld returned 1 exit status

fann_create_standard 在 fann.h 和 fann.c 上。由于fann.h被fixedfann.h包含了,而fann.h不应该直接被包含,我想我必须编译fann.c和fixedfann.c,然后链接(如果我做错了告诉我,我我仍然不熟悉这种“链接”的东西)。

所以我做了:

$ gcc fann/fixedfann.c -o fann/fixedfann.o
fann/fixedfann.c:22:20: fatal error: config.h: No such file or directory
compilation terminated.

然后我做了:

$ gcc fann/fixedfann.c -o fann/fixedfann.o -include fann/include/config.h 
fann/fixedfann.c:22:20: fatal error: config.h: No such file or directory
compilation terminated.

现在,为什么在这里找不到 config.h 文件?

--更新

感谢@JonathanLeffler,我可以在这里采取一些措施。但现在我被困在:

$ gcc fann/fixedfann.c -o fann/fixedfann.o -I./fann/include/ -lm
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

并且,使用 grep,我在 fann 文件夹中找不到任何对 main 的引用...也没有函数 _start,我不知道是谁链接此 crt1.o...知道这里出了什么问题吗?

--更新2

好的,我使用 Harmeet 的 Makefile 获得了 .o 文件,现在我正在尝试链接所有内容。 我用 gcc -c main.c 创建了 main.o,我尝试了:

gcc -o output main.o fann/fixedfann.o -lm

(-lm 用于 libmath,这是必需的)我得到了:

main.c:(.text+0xba): undefined reference to `fann_train_on_file'

这个 fann_train_on_filefann_train_data.c 上,所以我尝试了:

gcc -o output main.o fann/fixedfann.o fann/fann_train_data.o -lm

但是我遇到了很多 multiple definition of... 错误... :/

看起来 fann_train_data.o 已经包含/链接,但如果是这样,为什么找不到 fann_train_on_file

--更新3

我仍然真的被困在这里......知道这两行中的哪一行(如果有的话)应该起作用吗?:

gcc -o output main.o hello.o fann/fixedfann.o fann/fann_train_data.o -lm

gcc -o output main.o hello.o fann/fixedfann.o -lm

--Harmeet 更新

输出是:

$ make
gcc -L./fann -lfann main.o -o main
main.o: In function `main':
main.c:(.text+0x62): undefined reference to `fann_create_standard'
main.c:(.text+0x7a): undefined reference to `fann_set_activation_function_hidden'
main.c:(.text+0x8e): undefined reference to `fann_set_activation_function_output'
main.c:(.text+0xba): undefined reference to `fann_train_on_file'
main.c:(.text+0xce): undefined reference to `fann_save'
main.c:(.text+0xda): undefined reference to `fann_destroy'
collect2: ld returned 1 exit status
make: *** [main] Error 1

最佳答案

您可以使用 ar 制作静态库并使用它。

在您的 hello-fann-3/fann/ 文件夹下创建一个 Makefile,包含以下内容 -

SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
CFLAGS = -c -Iinclude

all: libfann.a

libfann.a: $(OBJECTS)
    ar rcs $@ $^

%.o: %.c
    gcc $(CFLAGS) $^

然后在hello-fann-3/fann/中使用make命令构建静态库。上面的 Makefile 将生成可以链接到您的程序的 libfann.a

在您的 hello-fann-3/ 文件夹下创建一个 Makefile,包含以下内容 -

SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
CFLAGS = -c -I./fann/include
LFLAGS = -L./fann -lfann

main: $(OBJECTS)
    gcc $(LFLAGS) $^ -o $@

%.o: %.c
    gcc $(CFLAGS) $^

然后在hello-fann-3/中使用make命令构建main程序。

在您的 main.c 中,您必须包含 fan.h,例如 -

#include "fann.h"

如果您不了解 Makefile,可以在这里阅读 -

http://www.gnu.org/software/make/manual/html_node/index.html

关于c - 如何编译这个库以供使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171445/

相关文章:

c++ - 为什么 GCC 交叉编译器不能找到所有的库?

c++ - 在 Visual Studio 中链接 PostgreSQL 自定义 C++ 项目时出错

C 在 Union 中改变值

c - 作业 3.exe : 0xC0000005: Access violation reading location 0x33772c58 中 0x77ea15de 处出现未处理的异常

c - 在 C 中处理 stdin 上的输入

c++ - 使用 RcppArmadilloExtensions/sp_mat.h 从矩阵到 Armadillo 的稀疏矩阵转换

ios - clang : error: linker command failed with exit code 1 (use -v to see invocation) with Quickblox

c++ - Objective-C:避免_作为局部变量名的技术原因?

c++ - cmake生成汇编文件然后编译成可执行文件

c++ - GCC 4.5.2 链接器在使用异常时出现问题 (C++)