c - "undefined reference"C编译静态库错误

标签 c gcc

我有一个文件 main.c、一个头文件 rippledp.h 和一个库 rippledp.a。问题是:当我执行“make”命令时,我得到这个输出:

g++ -O2 -DNDEBUG -static   -o rippledp main.o rippledp.a -lm -pthread
main.o: In function `main':
main.c:(.text+0x24): undefined reference to `rippledp_read'
main.c:(.text+0x39): undefined reference to `rippledp'
main.c:(.text+0x43): undefined reference to `rippledp_write'
collect2: ld returned 1 exit status
make: ** [rippledp] Erro 1

这是生成文件:

#--------------------------------------------------#
#  Ripple-DP (ISPD2015 contest version)            #
#  Copyright (c) 2015                              #
#  Department of Computer Science and Engineering  #
#  The Chinese Univeristy of Hong Kong             #
#                                                  #
#  Contact:                                        #
#  Wing-Kai Chow <wkchow@cse.cuhk.edu.hk>          #
#  Evangeline F.Y. Young <fyyoung@cse.cuhk.edu.hk> #
#--------------------------------------------------#

OPT= -O2 -DNDEBUG
#OPT= -O0 -ggdb
TYPE= -static
#WFLAG= -Wall -Winline

CC= g++ $(OPT) $(TYPE) $(WFLAG) $(DEBUG)

LIBS= -lm -pthread

SRCS = ${OBJS:%.o=%.c}
BFILE = rippledp

all:    $(BFILE)

#$(BFILE): main.o rippledp.a libdef.a liblef.a
#   $(CC) -o $(BFILE) main.o rippledp.a libdef.a liblef.a $(LIBS)

$(BFILE): main.o rippledp.a
    $(CC) -o $(BFILE) main.o rippledp.a $(LIBS)

%.o : %.c %.h
    $(CC) -c $*.c


clean:
    rm -f *.o $(BFILE) core

这是main.c:

#include "rippledp.h"

int main(int argc, char **argv){
    /* read benchmark files: tech.lef, cells.lef, floorplan.def */
    /* read global placement solution: placed.def */
    rippledp_read((char*) "tech.lef", (char*) "cells.lef", (char*) "floorplan.def", (char*) "placed.def");

    /* detailed placement with target utility and maximum displacement constraint */
    rippledp(0.8, 200000);

    /* write the detailed placement solution to output file */
    rippledp_write((char*)"dplaced.def");

    return 0;
}

这里是 rippledp.h:

/*--------------------------------------------------*/
/*  Ripple-DP (ISPD2014 contest version)              */
/*  Copyright (c) 2014                              */
/*  Department of Computer Science and Engineering  */
/*  The Chinese Univeristy of Hong Kong             */
/*                                                  */
/*  Contact:                                        */
/*  Wing-Kai Chow <wkchow@cse.cuhk.edu.hk>          */
/*  Evangeline F.Y. Young <fyyoung@cse.cuhk.edu.hk> */
/*--------------------------------------------------*/

#ifndef _RIPPLEDP_H_
#define _RIPPLEDP_H_

/*read benchmarks and placed global placement solution*/
void rippledp_read(char *tech_file, char *cell_file, char *floorplan_file, char *placed_file);

/*Perform displacement-constrained legalization and detailed placement*/
/* target_util = target utility */
/* max_disp    = maximum displacement constraint */
void rippledp(double target_util, double max_disp);

/*write placement result in DEF format*/
void rippledp_write(char *output_file);

#endif

我也试过手动编译链接。我首先编译使用:

gcc -c main.c

然后,我尝试了所有这些链接替代方案(我将 rippledp.a 重命名为 librippledp.a):

gcc -o out -L. -lrippledp main.o
gcc -o out -L. main.o -lrippledp 
gcc -o out main.o -L. -lrippledp 
gcc main.o -o out -L. -lrippledp 
gcc -o out -lrippledp -L. main.o
gcc -lrippledp -o out -L. main.o

输出是一样的。

我无权访问图书馆内容。

最佳答案

Your library是用 C++ 编译的,因此包含 C++ 损坏的名称。但是您将 main.c 编译为 C,因此它会寻找未损坏的名称,因此无法找到它们。将 main.c 重命名为 main.cpp 并使用 g++ 编译以修复此问题。

关于c - "undefined reference"C编译静态库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45870645/

相关文章:

c - statvfs() 和 statfs() 系统调用之间的区别?

android - 为什么这些后续的 "dup2"调用存在于 android 引导代码中

c - fscanf 循环无法正常工作

_sync_val_compare_and_swap 可以返回除 int 之外的任何值吗?

gcc - 函数序言中的 sub esp、eax 的作用是什么?

c++ - 在纯二进制应用程序中静态链接 libstdc++ 和 libgcc 是否合法?

c++ - 返回指针数组(CvSeq 指针)

c - 为什么 C 标准 C11 在 gcc 中不是默认的?

c - 如何从 GCC-6 中捕获类似 `-Wduplicated-cond` 的重复项

c - HW3.exe : 0xC0000005: Access violation reading location 0x00000004 中 0x012351CF 处出现未处理的异常