0

I have a file main.c, a header rippledp.h and a library rippledp.a. The problem is: when I execute the "make" command, I get this output:

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:

#--------------------------------------------------#
#  Ripple-DP (ISPD2015 contest version)            #
#  Copyright (c) 2015                              #
#  Department of Computer Science and Engineering  #
#  The Chinese Univeristy of Hong Kong             #
#                                                  #
#  Contact:                                        #
#  Wing-Kai Chow <[email protected]>          #
#  Evangeline F.Y. Young <[email protected]> #
#--------------------------------------------------#

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:

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

/*--------------------------------------------------*/
/*  Ripple-DP (ISPD2014 contest version)              */
/*  Copyright (c) 2014                              */
/*  Department of Computer Science and Engineering  */
/*  The Chinese Univeristy of Hong Kong             */
/*                                                  */
/*  Contact:                                        */
/*  Wing-Kai Chow <[email protected]>          */
/*  Evangeline F.Y. Young <[email protected]> */
/*--------------------------------------------------*/

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

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.

4
  • 3
    Does nm on the library actually show the symbols? Commented Aug 24, 2017 at 20:54
  • 1
    Are you sure you have rippledp.h that corresponds to rippledp.a? (try nm) Are you sure that rippledp.a has the right architecture? (try objdump -a). By the way -lrippledp never uses a file named rippledp.a, it can only use librippledp.a or librippledp.so. Commented Aug 24, 2017 at 21:41
  • If you use -lrippledp as an argument, doesn't it look for librippledp.a? Does make me wonder if the "rippledp.a" has been built right if it's not named with that normal lib....a form. Commented Aug 24, 2017 at 21:44
  • Run a clean operation (remove the object files and libraries that you build) and rerun the build. Record where it goes wrong, and what led up to the failure. Note that C code cannot call C++ functions unless those functions were declared extern "C" in the C++ code. That requires care — interworking between C and C++ requires care. Commented Aug 24, 2017 at 22:55

1 Answer 1

4

Your library is compiled with C++ and thus contains C++ mangled names. But you compiled main.c as C, so it looked for unmangled names and thus couldn't find them. Rename main.c to main.cpp and compile it with g++ to fix this issue.

Sign up to request clarification or add additional context in comments.

10 Comments

OK but why does the linker report unmangled function names? This should not normally happen in this situation. main.c seems to be compiled with gcc, g++ only does the linking.
@n.m. Names in error messages are demangled automatically. It would be quite annoying to decipher C++ errors otherwise.
@n.m. And the makefile defines CC to be g++ so main.c is compiled with that.
When ld demangles a C++ function name, it includes the signature. Not happening here. The question actually mentions explicitly that main.c is compiled by gcc (not CC, not g++).
@n.m. The %.o rule in the makefile will use g++.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.