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 <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:
#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 <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):
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. 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.
Related
I am struggling with building a program with multiple files whith inter files function calls with makefile in C. Let's say that I have a main file which call a function call_print_hello() declared in a header file fdeclar_macros.h and written in the file script1.c. The function call_print_hello() itself calls another function print_hello() also declared in fdeclar_macros.h and written in script2.c. I have also a makefile but when I run it I get the following error message:
gcc -g -Wall -c main.c
gcc -g -Wall -c script1.c
gcc -o main main.o script1.o
Undefined symbols for architecture x86_64:
"_call_print_hello", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
Here are the content of the files:
makefile:
CC = gcc
CFLAGS = -g -Wall
main: main.o script1.o
$(CC) -o main main.o script1.o
main.o: main.c fdeclar_macros.h
$(CC) $(CFLAGS) -c main.c
script2.o: script2.c fdeclar_macros.h
$(CC) $(CFLAGS) -c script2.c
script1.o: script1.c fdeclar_macros.h
$(CC) $(CFLAGS) -c script1.c
run: main
./main
clean:
$(RM) -rf justify *.dSYM *.o
main.c:
#include "fdeclar_macros.h"
int main(){
call_print_hello();
return 0;
}
fdeclar_macros.h:
#define NUMBER 3
void print_hello();
void call_print_hello();
script1.c:
#include <stdio.h>
#include "fdeclar_macros.h"
void print_hello(){
printf("hello %d\n", NUMBER);
}
script2.c:
#include "fdeclar_macros.h"
void call_print_hello(){
print_hello();
}
The make target for the main executable does not contain a dependency on script2.o and the rule to build main does not link script2.o into the main executable either.
So the linker tries to build an executable with the content of script2.o missing, but as that content is required, linking fails.
One easy fix would be to change the original rule
main: main.o script1.o
$(CC) -o main main.o script1.o
by adding script2.o:
main: main.o script1.o script2.o
$(CC) -o main main.o script1.o script2.o
I will leave finding more general rules as an exercise to the reader.
NAME = my_programm
CC = gcc
CFLAGS = -Wall -Werror -Wextra
MY_SOURCES = main.c script1.c script2.c
MY_OBJECTS = $(MY_SOURCES:.c=.o)
$(NAME): $(MY_OBJECTS)
#cc $(CFLAGS) $(MY_OBJECTS) -o $(NAME)
clean:
#rm -f $(MY_OBJECTS)
#rm -f $(NAME)
run:
./my_programm
I wrote the following makefile:
MAIN = main
# Macro nome compilatore
CC = gcc
# Macro flag del compilatore
CFLAGS = -Wall
# Macro file oggetto
OBJS = $(MAIN).o
# Macro libreria matematica
LIBS = -lm
# Target
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) $(LIBS) -o $(MAIN)
When i run it for the first time, the output is:
gcc -Wall -c -o main.o main.c
gcc -Wall main.o -lm -o main
Can you explain me the first line please? What does it mean?
Thank you for your time.
I am trying to compile a minimal test shared library in FreeBSD using:
FreeBSD clang version 6.0.1 (tags/RELEASE_601/final 335540) (based on LLVM 6.0.1)
Target: x86_64-unknown-freebsd12.0
Thread model: posix
InstalledDir: /usr/bin
test.c
#include "test.h"
int SampleFunction(int a, int b)
{
return a * b;
}
test.h
#ifndef TESTLIB_H
#define TESTLIB_H
extern int SampleFunction(int a, int b);
#endif
Makefile
# Makefile TESTLIB
TEST_OBJS = test.o
TEST_HEADERS = test.h
TEST_LIB = test.so
CC = cc
testlib: $(TEST_OBJS)
$(CC) -fpic -o $(TEST_LIB) $(TEST_OBJS)
# Rebuilt if this Makefile or header changes
$(TEST_OBJS): Makefile $(TEST_HEADERS)
Output:
$ make testlib
cc -O2 -pipe -c test.c -o test.o
cc -fpic -o test.so test.o
/usr/bin/ld: error: undefined symbol: main
>>> referenced by crt1.c:76 (/usr/src/lib/csu/amd64/crt1.c:76)
>>> /usr/lib/crt1.o:(_start)
cc: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error code 1
Stop.
make: stopped in /usr/home/user/testlib
As well as adding -shared to the linker stage (as mentioned by Oo.oO), you probably also want to compile with the -fPIC flag as well. So if you are using the makefile implicit rules for the compiling (looks like you are) then you will probably want to add that flag to CFLAGS. I don't think you need it for the linking stage:
# Makefile TESTLIB
TEST_OBJS = test.o
TEST_HEADERS = test.h
TEST_LIB = test.so
CFLAGS += -fPIC
CC = cc
testlib: $(TEST_OBJS)
$(CC) -shared -o $(TEST_LIB) $(TEST_OBJS)
# Rebuilt if this Makefile or header changes
$(TEST_OBJS): Makefile $(TEST_HEADERS)
I prefer using the OS-provided bsd.lib.mk for such building libraries. The following should do the right thing -- whatever it may be:
SHLIB_NAME= testlib
SRCS= test.c
.include <bsd.lib.mk>
(For building executables there is bsd.prog.mk, BTW.)
I am trying to use matlab libmat.dll in a C application. To compile my C application I use MinGW, for now I use matlab exemple "matcreate.c" and try to compile it, so the projects consist of only one file : main.c .
Here is the makefile I use :
MATINCLUDE = "C:\Program Files\MATLAB\R2010a\extern\include"
MATLIBRARY = "C:\Program Files\MATLAB\R2010a\bin\win64"
#
CC = gcc
LD = gcc
CFLAGS = -O3 -Wall
LFLAGS = -Wall -O3
LIBS = -I$(MATINCLUDE) -L$(MATLIBRARY)
#
PROG = matTest
LISTEOBJ = \
main.o
.c.o :
$(CC) -c $(CFLAGS) $(LIBS) -o $# $<
all : $(PROG)
$(PROG) : $(LISTEOBJ)
$(LD) -o $(PROG) $(LFLAGS) $(LISTEOBJ) $(LIBS)
clean :
rm -f *.obj
Here is what I get in the console
E:\Users\Desk\Dropbox\matTest>make
gcc -c -O3 -Wall -I"C:\Program Files\MATLAB\R2010a\extern\include" -L"C:\Pr
ogram Files\MATLAB\R2010a\bin\win64" -o main.o main.c
gcc -o Hello_world -Wall -O3 main.o -I"C:\Program Files\MATLAB\R2010a\extern\i
nclude" -L"C:\Program Files\MATLAB\R2010a\bin\win64"
main.o:main.c:(.text.startup+0x48): undefined reference to `matOpen'
main.o:main.c:(.text.startup+0x6e): undefined reference to `mxCreateDoubleMatrix
_730'
e:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: main.o: ba
d reloc address 0x6e in section `.text.startup'
e:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
make: *** [Hello_world] Error 1
Why do I have "undefined reference to matOpen'" and "undefined reference to mxCreateDoubleMatrix" ?? those function are declared in mat.h. and I added #include "mat.h" to the begining of main.c
thank you
Looks like you have included the path to the matlab library, but not the library itself. You need to add a -l<libraryname> to your link line.
I use crypt() function and the compile flag named -lcrypt the problem is that the compiler says undefined reference to crypt(). Can anyone tell me what I'm doing wrong?
Makefile
CC = gcc
CFLAGS=-Wall -lm -lcrypt
OBJS = get_passwords_hashed.o
PROG = get_passwords_hashed.exe
#adicionar or mudar o OBJS se tiver outras files para o programa
#GENERIC
all: ${PROG}
clean:
rm ${OBJS} *~ ${PROG}
${PROG}: ${OBJS}
${CC} ${OBJS} -o $#
.c.o:
${CC} $< -c -o $#
# $# - turns .c into .o
###################################
#dependencias
so_final.o: get_passwords_hashed.c get_passwords_hashed.h
main.c
#include <stdio.h>
#include <string.h>
#include <crypt.h>
int testar_pass(char ant[],char (*pointer_hashes)[max_chars_string]){ // ponteiro para array de chars - char ** ant
char * password ;
char * encrypted;
password = malloc(strlen(ant)*sizeof(char)); //password calculada recebida anteriror
encrypted = malloc(strlen(ant)*sizeof(char));//hash
strcpy(password,ant);
encrypted = crypt(password,"10");
if(strcmp(*pointer_hashes,encrypted) == 0){
return 1;
}
else return 0;// devolve erro
}
Pass -lm -lcrypt at the end of your compilation line.
LIBS=-lm -lcrypt
${CC} ${OBJS} -o $# ${LIBS}
EDIT:
The explanation of why it makes a difference (as requested in a comment) from gcc manual:
-llibrary
[...]
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the
order they are specified.
Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file ‘foo.o’ but before ‘bar.o’. If ‘bar.o’ refers to functions in ‘z’, those functions
may not be loaded.