nit*_*ian 2 c compiler-construction linker linker-errors
我的主目录中有以下目录.
资源
包括
库
在源目录中,我有以下文件:
a.c
#include <stdio.h>
#include "a.h"
extern void function(void);
int main()
{
printf("PREDEFINED = %d\n",PREDEFINED);
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
b.c
#include <stdio.h>
void function()
{
printf("Hello from function\n");
}
Run Code Online (Sandbox Code Playgroud)
a.h
#define PREDEFINED 100
Run Code Online (Sandbox Code Playgroud)
我编译了bc并将bo移动到Lib文件夹.
然后我从source文件夹中尝试了以下命令
gcc -Wall -o temp a.c -I../include -L../Lib
Run Code Online (Sandbox Code Playgroud)
但它显示以下错误a.c: undefined reference to函数`
`
但是当我使用以下命令时,它会生成输出文件temp而没有任何错误
gcc -Wall -o temp a.c -I../include ../Lib/b.o
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
目标文件不是库.试试这个来创建和使用一个简单的静态库:
ar rcs ../Lib/libb.a b.o
gcc -Wall -o temp a.c -I../include -L../Lib -lb
Run Code Online (Sandbox Code Playgroud)
静态库通常包含多个对象.您可以将完整的对象列表添加到该ar行.请注意,该选项-l{name}会使链接器查找名为的文件lib{name}.a.