簡體   English   中英

使用C編譯靜態庫時出現“未定義引用”錯誤

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

我有一個文件main.c,一個頭文件ripledp.h和一個庫漣漪p.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

這是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

這是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;
}

這是漣漪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

我也嘗試手動編譯和鏈接。 我首先使用以下代碼進行編譯:

gcc -c main.c

然后,我嘗試了所有這些替代方法來進行鏈接(我將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

和輸出是相同的。

我無權訪問圖書館的內容。

您的庫是使用C ++編譯的,因此包含C ++雜亂的名稱。 但是您將main.c編譯為C,因此它查找的是未修改的名稱,因此找不到它們。 main.c重命名為main.cpp並使用g ++進行編譯以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM