gpt4 book ai didi

c - 创建静态库时生成文件中的 undefined reference 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:30:08 24 4
gpt4 key购买 nike

我有 4 个 .c 文件 hello.chere.cbye.cmain.c。一个头文件 mylib.h

内容如下

你好.c

#include<stdio.h>

void hello()
{
printf("Hello!\n");
}

这里.c

#include<stdio.h>

void here()
{
printf("I am here \n");
}

再见

#include<stdio.h>

void bye()
{
printf("Bye,Bye");
}

主程序

#include<stdio.h>
#include "mylib.h"

int main()
{

hello();
here();
bye();
return 1;
}

mylib.h

#ifndef _mylib_
#define _mylib_

void hello();
void here();
void bye();

#endif

创建静态库的 makefile 是:生成文件

all:    myapp

#Macros

#Which Compiler
CC = gcc

#Where to install
INSTDIR = /usr/local/bin

#Where are include files kept
INCLUDE = .

#Options for developement
CFLAGS = -g -Wall -ansi

#Options for release
#CFLAGS = -O -Wall -ansi

#Local Libraries
MYLIB = mylib.a

myapp: main.o $(MYLIB)
$(CC) -o myapp main.o $(MYLIB)

$(MYLIB): hello.o here.o bye.o
ar rcs $@ $^

main.o: main.c mylib.h
hello.o: hello.c
here.o: here.c
bye.o: bye.c

clean:
-rm main.o hello.o here.o bye.o $(MYLIB)

install: myapp
@if [ -d $(INSTDIR) ]; \
then \
cp myapp $(INSTDIR);\
chmod a+x $(INSTDIR)/myapp;\
chmod og-w $(INSTDIR)/myapp;\
echo "Installed in $(INSTDIR)";\
else \
echo "Sorry, $(INSTDIR) does not exist";\
fi

问题:当我执行命令时

make -f Makefile all 

我得到错误: gcc -o myapp main.o mylib.a

main.o: In function `main':

/home/usr/molly/main.c:7: undefined reference to `hello()'

/home/usr/molly/main.c:8: undefined reference to `here()'

/home/usr/molly/main.c:9: undefined reference to `bye()'

main.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'

collect2: ld returned 1 exit status

make: *** [myapp] Error 1

问题:我该如何解决这个问题?为什么会有 undefined reference

最佳答案

这实际上对我有用。尝试 rm mylib.a 然后 make

关于c - 创建静态库时生成文件中的 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3946376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com