简体   繁体   English

使用C编译静态库时出现“未定义引用”错误

[英]“undefined reference” error compiling a static library in C

I have a file main.c, a header rippledp.h and a library rippledp.a. 我有一个文件main.c,一个头文件ripledp.h和一个库涟漪p.a。 The problem is: when I execute the "make" command, I get this output: 问题是:当我执行“ 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

Here is the Makefile: 这是Makefile:

#--------------------------------------------------#
#  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

Here is main.c: 这是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;
}

And here is rippledp.h: 这是涟漪p.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

I also tried to compile and link manually. 我也尝试手动编译和链接。 I first compiled using: 我首先使用以下代码进行编译:

gcc -c main.c

Then, I tried all these alternatives for linking (I renamed rippledp.a to librippledp.a): 然后,我尝试了所有这些替代方法来进行链接(我将rippedp.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

and the output was the same. 和输出是相同的。

I dont have access to the library content. 我无权访问图书馆的内容。

Your library is compiled with C++ and thus contains C++ mangled names. 您的库是使用C ++编译的,因此包含C ++杂乱的名称。 But you compiled main.c as C, so it looked for unmangled names and thus couldn't find them. 但是您将main.c编译为C,因此它查找的是未修改的名称,因此找不到它们。 Rename main.c to main.cpp and compile it with g++ to fix this issue. main.c重命名为main.cpp并使用g ++进行编译以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM