【问题标题】:SHA1 library link not found using makefile使用 makefile 找不到 SHA1 库链接
【发布时间】:2013-03-26 18:33:30
【问题描述】:

我在编译时无法将 sha 库与我的 makefile 链接起来。

这是我的生成文件:

CFLAGS= -g -Wall -Werror -std=c99 -pedantic 
LDFLAGS=-lssl -lcrypto
CC = gcc
LD = gcc
OBJS = dhtnode.o
PROG = dhtnode

.c.o:
gcc $< -o $@ $(CFLAGS)

all: $(PROG)

$(PROG): $(OBJS)
$(LD) $(LDFLAGS) $(OBJS) -o $(PROG)

dhtnode.o: dhtnode.c dhtpackettypes.h
$(CC) $(CFLAGS) $(LDFLAGS) dhtnode.c

clean:
/bin/rm -f *.o dhtnode

我使用 lcrypto 库的函数在这里:

#include <openssl/sha.h>
#include <stdlib.h>
#include <stdin.h>
//there are other includes but not concerning this part of the code

char sha() {
char *ibuf = malloc(sizeof(char));
ibuf ="172.0.0.1:11112";
char *obuf = malloc(SHA_DIGEST_LENGTH);

SHA1((unsigned char*)ibuf, strlen(ibuf), (unsigned char*)obuf);
int i;
for (i = 0; i < 20; i++) {
    printf("%x" , (unsigned char)obuf[i]);
    }
printf("\n");

return *ibuf;
}

这是我在使用 Eclipse 构建时遇到的错误:

C/p2p/dhtnode.c:107: undefined reference to `SHA1'

谁能告诉我我的 makefile 或可能的 eclipse 设置有什么问题?

提前谢谢!

【问题讨论】:

    标签: c makefile sha1


    【解决方案1】:

    编译目标文件时,您不需要LDFLAGS。您还需要 -c 编译器标志来生成目标文件,而不是链接二进制文件:

    dhtnode.o: dhtnode.c dhtpackettypes.h
        $(CC) $(CFLAGS) -c dhtnode.c
    

    进行此更改后,程序为我成功编译和链接。

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      相关资源
      最近更新 更多