This may be a simple answer but I am trying to compile code for a simple User level file system. I am running my code on a windows Ubuntu subsystem.
I have all the lpthread and lreadline libraries updated and installed and I still get the undefined reference when compiling.
gcc -Wall -g -lreadline -lcurses -lpthread userfs.c parse.c crash.c -o userfs
/tmp/ccNrZDqQ.o: In function `main':
/home/kupinah/userfs/userfs.c:75: undefined reference to `readline'
/tmp/ccwcrZEh.o: In function `init_crasher':
/home/kupinah/userfs/crash.c:10: undefined reference to `pthread_create'
/home/kupinah/userfs/crash.c:14: undefined reference to `pthread_detach'
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'userfs' failed
make: *** [userfs] Error 1
Here is the code locations and headers included for each.
userfs.c:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <assert.h>
#include <string.h>
#include "parse.h"
#include "userfs.h"
#include "crash.h"
...
...
...
while(1) {
cmd_line = readline(buildPrompt());
if (cmd_line == NULL) {
fprintf(stderr, "Unable to read command\n");
continue;
}
...
...
makefile
CC = gcc
COMPILER_WARNINGS = -Wall
GDB_FLAGS = -g
GCOV_FLAGS = -fprofile-arcs -ftest-coverage
GPROF_FLAGS = -pg -a
LD_LIBS = -lreadline -lcurses -lpthread
CFLAGS = $(COMPILER_WARNINGS) $(GDB_FLAGS) $(LD_LIBS)
all: userfs
userfs: userfs.c parse.c crash.c
$(CC) $(CFLAGS) userfs.c parse.c crash.c -o userfs
clean:
/bin/rm -f userfs *.o *~
Help pls.
#MadScientist Helped me out on this one.
What fixed this issue was some simple edits to the make file.
CC = gcc
COMPILER_WARNINGS = -Wall
GDB_FLAGS = -g
GCOV_FLAGS = -fprofile-arcs -ftest-coverage
GPROF_FLAGS = -pg -a
LD_LIBS = -lpthread -lreadline -lcurses
CFLAGS = $(COMPILER_WARNINGS) $(GDB_FLAGS)
all: userfs
userfs: userfs.c parse.c crash.c
$(CC) $(CFLAGS) userfs.c parse.c crash.c -o userfs $(LD_LIBS)
clean:
/bin/rm -f userfs *.o *~
Changes:
LD_LIBS = -lpthread -lreadline -lcurses---- Reordering
CFLAGS = $(COMPILER_WARNINGS) $(GDB_FLAGS)---- Removed $(LD_LIBS)
userfs: userfs.c parse.c crash.c
$(CC) $(CFLAGS) userfs.c parse.c crash.c -o userfs $(LD_LIBS)----Added $(LD_LIBS)
Related
I am trying to take a binary to decimal function out of my main.c file and into its own file, but when I compile the files with my makefile I get the error:
undefined reference to `btod'
Here is my main.c file:
#include "btod.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
printf("\nEnter a binary number: ");
char c[100];
scanf("%s",c);
printf("Number converted to decimal: %d\n\n\n",btod(strlen(c),c));
return 0;
}
Here is my btod.c file:
#include <stdlib.h>
#include <string.h>
int btod(int size,char inputBin[size])
{
int i,num=0;
for(i=0;i<size;i++)
{
num=num*2+(inputBin[i]-48);
}
return num;
}
Here is my btod.h file:
int btod(int size,char inputBin[size]);
And lastly, here is my makefile:
CC = gcc
INCLUDE = -I.
CFLAGS = -g -Wall
LDFLAGS = -L. \
-L/usr/lib
LDLIBS = \
-lc -lm
.c.o:
$(CC) $(INCLUDE) $(CFLAGS) -c $<
all: main
main: main.o
$(CC) -o $# $^ $(LDFLAGS) $(LDLIBS)
btod: btod.o
$(CC) -o $# $^ $(LDFLAGS) $(LDLIBS)
clean:
rm -f *.o
rm -f main
I am thinking it might have to do with the btod.c file not being compiled properly within the makefile but I cannot figure out what is incorrect about it.
The error is because you don't link in btod.o when building main. If you use GNU Make, you can simplify your Makefile to just a few lines:
.PHONY: all clean
CFLAGS = -g -Wall
all: main
clean:
rm -f *.o main
main: btod.o main.o
In btod.c use '0' instead of 48. In main.c remove the line int n.
I know that you have to create a header file and #include it in your main. I have done that and when I compile my code for some reason it is unable to figure out where one of my functions are.
The layout of my project is a: threads.c makeCityFromInput.h and makeCityFromInput.c
I have the threads.c #including the makeCityFromInput.h
This is what happens when I try to compile:
make
gcc -ggdb -std=c99 -Wall -Wextra -pthread -c threads.c
gcc -ggdb -std=c99 -Wall -Wextra -pthread -o threads threads.o -lm -lncursesw -pthread
threads.o: In function `main':
/home/project3/threads.c:183: undefined reference to `readConfig'
collect2: error: ld returned 1 exit status
Makefile:71: recipe for target 'threads' failed
make: *** [threads] Error 1
It is unable to find my readconfig file even though it is in my header file
my threads.c file
#define _DEFAULT_SOURCE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <ncurses.h> //displays the cars
#include <pthread.h> //threading
#include <unistd.h>
#include "makeCityFromInput.h"
#define MAX 2048
int main(int argc, char *argv[]){
//Expected input: Name of config file
// If the config file is not given; or too many config files are given
if (argc != 2){
endwin();
fprintf(stderr, "Usage: Enter a configuration file");
exit( EXIT_FAILURE );
}
FILE *fp = NULL;
fp = fopen(argv[1], "r");
assert( fp );
readConfig(fp);
fclose( fp );
return 0;
}
my makeCityFromInput.h file:
#ifndef _MAKECITYFROMINPUT_H
#define _MAKECITYFROMINPUT_H
void readConfig(FILE *fp);
int *parse_line(char *line, int *numInts);
void create_skyline(int *ground, int size, int maxMissiles);
#endif
my makeCityFromInput.c file:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <ncurses.h> //displays the cars
#include <pthread.h> //threading
#include <unistd.h>
void readConfig(FILE *fp){
...
}
My make file
#
# Created by gmakemake (Ubuntu Jul 25 2014) on Wed Nov 14 20:05:05 2018
#
#
# Definitions
#
.SUFFIXES:
.SUFFIXES: .a .o .c .C .cpp .s .S
.c.o:
$(COMPILE.c) $<
.C.o:
$(COMPILE.cc) $<
.cpp.o:
$(COMPILE.cc) $<
.S.s:
$(CPP) -o $*.s $<
.s.o:
$(COMPILE.cc) $<
.c.a:
$(COMPILE.c) -o $% $<
$(AR) $(ARFLAGS) $# $%
$(RM) $%
.C.a:
$(COMPILE.cc) -o $% $<
$(AR) $(ARFLAGS) $# $%
$(RM) $%
.cpp.a:
$(COMPILE.cc) -o $% $<
$(AR) $(ARFLAGS) $# $%
$(RM) $%
CC = gcc
CXX = g++
RM = rm -f
AR = ar
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
LINK.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS)
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) -c
COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
CPP = $(CPP) $(CPPFLAGS)
########## Flags from header.mak
CFLAGS = -ggdb -Wall -Wextra -pedantic -std=c99 -pthread
# program uses pthreads and curses libraries
CLIBFLAGS = -lncursesw -lpthread -lrt
########## End of flags from header.mak
CPP_FILES =
C_FILES = pt-cruisers.c racer.c
PS_FILES =
S_FILES =
H_FILES = racer.h
SOURCEFILES = $(H_FILES) $(CPP_FILES) $(C_FILES) $(S_FILES)
.PRECIOUS: $(SOURCEFILES)
OBJFILES = racer.o
#
# Main targets
#
all: pt-cruisers
pt-cruisers: pt-cruisers.o $(OBJFILES)
$(CC) $(CFLAGS) -o pt-cruisers pt-cruisers.o $(OBJFILES) $(CLIBFLAGS)
#
# Dependencies
#
pt-cruisers.o: racer.h
racer.o: racer.h
#
# Housekeeping
#
Archive: archive.tgz
archive.tgz: $(SOURCEFILES) Makefile
tar cf - $(SOURCEFILES) Makefile | gzip > archive.tgz
clean:
-/bin/rm -f $(OBJFILES) pt-cruisers.o core
realclean: clean
-/bin/rm -f pt-cruisers
It looks like this
gcc -ggdb -std=c99 -Wall -Wextra -pthread -c threads.c
gcc -ggdb -std=c99 -Wall -Wextra -pthread -c makeCityFromInput.c
gcc -ggdb -std=c99 -Wall -Wextra -pthread -o threads threads.o makeCityFromInput.o -lm -lncursesw -pthread
If you show your make file we can also help with that...
I guess what you are actually asking is "What is wrong with my makefile?". As you have not provided it I can only take an educated guess. Here is a GNUmake snippet to build your program:
.PHONY: default
default: threads
CC := ...
LD := ...
CFLAGS := ...
LDFLAGS := ...
LIBS := ...
SRCS := threads.c makeCityFromInput.c
OBJS := $(SRCS:%.c=%.o)
$(OBJS): %.o: %.c
$(CC) $(CFLAGS) -o $# -c $<
threads: $(OBJS)
$(LD) $(LDFLAGS) -o $# $^ $(LIBS)
I installed fftw3.3.6 on my Ubuntu 16.04 to test the performance of using this library with thread and float enabled.
Step 1 :
Installed the library with thread and float and SIMD instructions enabled:`
sudo ./configure --enable-float --enable-generic-simd128 --enable-generic-simd256 --enable-threads
make
make install
Step 2 :
I wrote this code (basing on the manual and tutorial) to compute an fft of 1024 points using 4 threads (complex to complex):
#include <stdio.h>
#include <stdlib.h>
#include <fftw3.h>
#include "input.h"
#define NUMBER_OF_ELEMENTS 1024
#define NUM_THREADS 4
void Load_inputs(fftwf_complex* data)
{
int i;
for(i=0;i<NUMBER_OF_ELEMENTS;i++)
{
data[i][0] = input_data[2 * i];
data[i][1] = input_data[2 * i + 1];
}
}
int main()
{
fftwf_complex array[NUMBER_OF_ELEMENTS];
fftwf_plan p;
int i;
fftwf_init_threads();
fftwf_plan_with_nthreads(NUM_THREADS);
p = fftwf_plan_dft(1,NUMBER_OF_ELEMENTS,array,array,FFTW_FORWARD,FFTW_EXHAUSTIVE);
Load_inputs(array); //function to load input data from input.h file to array[]
fftwf_execute(p);
FILE* res = NULL;
res = fopen("result.txt", "w");
for ( i = 0; i <1024; i++ )
{
fprintf(res,"RE = %f \t IM = %f\n",array[i][0], array[i][1] );
}
fclose(res);
fftwf_destroy_plan(p);
fftwf_cleanup_threads();
}
And then, I compiled this program with this makefile.
CC=gcc
CFLAGS=-g3 -c -Wall -O0 -mavx -mfma -ffast-math
SOURCES=$ test.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=test
all: $(TASKMAP) $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $# -L -lfftw3f_threads -lfftw3f
.c.o:
$(CC) $(CFLAGS) $< -lm -o $#
clean:
rm -fr $(OBJECTS) $(EXECUTABLE)
Compilation errors :
After compilation I've got these errors:
gcc test.o -o test -L -lfftw3f_threads -lfftw3f
//usr/local/lib/libfftw3f.a(mapflags.o): In function `fftwf_mapflags':
mapflags.c:(.text+0x346): undefined reference to `__log_finite'
Makefile:13: recipe for target 'test' failed
//usr/local/lib/libfftw3f.a(trig.o): In function `cexpl_sincos':
trig.c:(.text+0x2c1): undefined reference to `sincos'
//usr/local/lib/libfftw3f.a(trig.o): In function `fftwf_mktriggen':
trig.c:(.text+0x50b): undefined reference to `sincos'
trig.c:(.text+0x653): undefined reference to `sincos'
test.o: In function `main':
/home/anouar/workspace/Thread_example//test.c:27: undefined reference to `fftwf_init_threads'
/home/anouar/workspace/Thread_example//test.c:28: undefined reference to `fftwf_plan_with_nthreads'
/home/anouar/workspace/Thread_example//test.c:40: undefined reference to `fftwf_cleanup_threads'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
Is there something missing, or something wrong that I have did during installation and compilation?
Read the fine manual. From the sincos() man page:
Link with -lm.
Using -lm in the compile phase of your program is useless:
.c.o:
$(CC) $(CFLAGS) $< -lm -o $#
-lm needs to be in the link stage:
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $# -L -lfftw3f_threads -lfftw3f -lm
In my case, the problems with
undefined reference to `__log_finite'
could be solved by compiling without the -ffast-math option.
I have created a new makefile
CC = /usr/bin/gcc
MQM_HOME = /opt/mqm
CFLAGS = -g -Wall \
-I$(MQM_HOME)/inc \
-I/usr/include
#IBM_LIBPATH=-L/usr/vacpp/lib
IBM_LIBS= -liconv
MQ_LIBPATH=-L$(MQM_HOME)/lib64
MQLIB = $(MQ_LIBPATH) -lmqm -lmqmcs -lmqmzse
LIBS = $(IBM_LIBS) $(MQLIB) -lpthreads
#LDFLAGS = -q64 $(IBM_LIBPATH) $(MQ_LIBPATH)
SOURCE=/home/avalanche/oleg
default: ctm_mq_con_ex
ctm_mq_con_ex: ctm_mq_con_ex.o
# Compilation rules
EXE = $(SOURCE)/ctm_mq_con_ex
MAIN = $(SOURCE)/ctm_mq_con_ex.c
OBJS = $(SOURCE)/ctm_mq_con_ex.o
.c.o:
$(CC) $(CFLAGS) -c $(MAIN)
$(CC) $(CFLAGS) -o $(EXE) $(OBJS)
clean:
\rm -f $(OBJS)
\rm -f $(EXE)
Now it shows errors
make -f ./ctm_mq_con_ex.mk
/usr/bin/gcc -g -Wall -I/opt/mqm/inc -I/usr/include -c /home/avalanche/oleg/ctm_mq
/usr/bin/gcc -g -Wall -I/opt/mqm/inc -I/usr/include -o /home/avalanche/oleg/ctm_mq
/home/avalanche/oleg/ctm_mq_con_ex.o: In function `main':
/home/avalanche/oleg/ctm_mq_con_ex.c:67: undefined reference to `MQCONNX'
/home/avalanche/oleg/ctm_mq_con_ex.c:86: undefined reference to `MQOPEN'
/home/avalanche/oleg/ctm_mq_con_ex.c:114: undefined reference to `MQINQ'
/home/avalanche/oleg/ctm_mq_con_ex.c:142: undefined reference to `MQCLOSE'
/home/avalanche/oleg/ctm_mq_con_ex.c:163: undefined reference to `MQDISC'
collect2: ld returned 1 exit status
But in my C program I put header files
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
/* includes for WebSphere MQ */
#include <cmqc.h> /* For regular MQI definitions */
#include <cmqxc.h> /* For MQCD definition */
Did I miss something?
The error I'm getting:
undefined reference to `readline'
Here is my makefile:
all: stest stestdebug
stest: stest.o struct.o
gcc -g stest.o struct.o -lreadline -lncurses -o stest
stest.o: stest.c struct.h
gcc -g -c stest.c
stestdebug: stestdebug.o struct.o
gcc -g stestdebug.o struct.o -o stestdebug
stestdebug.o: stest.c struct.h
gcc -g -c stest.c -o stestdebug.o
struct.o: struct.c struct.h
gcc -g -c -DDEBUG struct.c
clean:
rm -f *.o stest stestdebug
docs:
doxygen
chmod a+r html/*
cp -p html/* ~/public_html/cs2303assig4
I've already imported all the necessary libraries for readline but am still getting this error.
Here is the code where I call it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "struct.h"
void requestInput() {
printf("Please fill out all prompts to create a new emplyoee.\n");
char *name = readline("Name:");
}
You are not linking to them in your main executable.
Put your libraries in a variable, and use them both in your test target and the normal target.
Have a look into LDFLAGS.