Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言构建静态库_C_Gcc_Dll_Static - Fatal编程技术网

用C语言构建静态库

用C语言构建静态库,c,gcc,dll,static,C,Gcc,Dll,Static,我想将两个源代码合并到一个C静态库中 renderay_core.c renderay_shapes.c 及其相应的头文件。为了避免这里的错误,我首先将其编译为独立的(非库) test.c #include <stdio.h> #include "renderay_core.c" #include "renderay_shapes.c" int main(void){ Canvas* canvas = new_Canvas(5,5); printf("Test"); }

我想将两个源代码合并到一个C静态库中

renderay_core.c
renderay_shapes.c
及其相应的头文件。为了避免这里的错误,我首先将其编译为独立的(非库)

test.c

#include <stdio.h>
#include "renderay_core.c"
#include "renderay_shapes.c"

int main(void){ 
  Canvas* canvas = new_Canvas(5,5);
  printf("Test");
}
而且效果很好

现在我要把它打包成一个静态库。按照以下步骤进行:

gcc -c renderay_core.c renderay_shapes.c
现在,我已经准备好将对象链接为库

ar rcs librenderay.a renderay_core.c renderay_shapes.c
是我用于此操作的命令。然后我尝试用库而不是普通的源文件编译test.c

gcc test.c -o main.exe -static -L -lrenderay
现在,当我尝试编译此文件时,会收到一条错误警告:

对“新画布”的未定义引用


告诉我链接到库失败了。我做错了什么?我遗漏了什么?

编译-不链接-源文件

gcc -c renderay_core.c -o renderay_core.o
gcc -c renderay_shapes.c -o renderay_shapes.o
然后打包

ar -rcs librenderay.a renderay_core.o renderay_shapes.o
并使用

gcc test.c -o main.exe -static -L. -lrenderay

您需要
L
在此处指定非标准位置-当前目录。

问题在于您链接的是源文件,而不是目标文件

ar rcs librenderay.a renderay_core.c renderay_shapes.c
一定是这样的

ar rcs librenderay.a renderay_core.o renderay_shapes.o
另外,你可以用Makefile来做这件事

CFLAGS = -O2 -Wall -fPIC
OBJS = renderay_core.o renderay_shapes.o

.c.o:
    $(CC) $(CFLAGS) -c $<

librenderay.a: $(OBJS)
    $(AR) rcs librenderay.a $(OBJS)

.PHONY: clean
clean:
    $(RM) librenderay.a $(OBJS)
CFLAGS=-O2-壁-fPIC
OBJS=renderay\u core.o renderay\u shapes.o
.c.o.:
$(CC)$(CFLAGS)-c$<
librenderay.a:$(OBJS)
$(AR)rcs librenderay.a$(OBJS)
.假冒:干净
清洁:
$(RM)librenderay.a$(OBJS)

了解头文件。不要包含实现文件。您似乎是针对C源文件而不是目标文件运行
ar
。哦,我看到了一个。在导致问题的-L mybe之后…是的,您需要一个句点(
)将链接器指向当前目录。谢谢,我不知道that@xetra11:此外,您需要使用存档(
ar
)命令打包目标文件,而不是源文件。啊,好吧,有点打字错误。谢谢你,伙计
CFLAGS = -O2 -Wall -fPIC
OBJS = renderay_core.o renderay_shapes.o

.c.o:
    $(CC) $(CFLAGS) -c $<

librenderay.a: $(OBJS)
    $(AR) rcs librenderay.a $(OBJS)

.PHONY: clean
clean:
    $(RM) librenderay.a $(OBJS)