Undefined reference to 'readline' - C - c

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.

Related

Undefined reference to (readline, pthread_create, pthread_detach), makefile not including libraries

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)

How to include helper functions in C?

There are 4 files:
helper.h //contains the signatures of functions in helper.c
helper.c //implements the signatures in helper.h
file.h //has all the includes needed to run file.h
file.c //this file includes file.h and helper.h
In file.c, I need to use the function that is defined in helper.c in my main function. However, file.c is saying that there is an undefined reference to 'func_found_in_helper.c'
Is this structure correct?
Yes, provided file.c contains
#include "helper.h"
and when building your program you link together helper.o and file.o.
You also need to ensure you compile each of the files with -c so that the compiler only compiles (and not links); do the link later with all the object files.
Here's a working example (I don't actually need a main.h but if you have one of those, #include it from main.c):
main.c
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
int
main (int argc, char **argv)
{
test ();
exit (0);
}
helper.c
#include <stdio.h>
void
test ()
{
printf ("Hello world\n");
}
helper.h
void test ();
To compile
gcc -Wall -Werror -c -o main.o main.c
gcc -Wall -Werror -c -o helper.o helper.c
To link
gcc -Wall -Werror -o test main.o helper.o
In a Makefile
test: main.o helper.o
gcc -Wall -Werror -o test main.o helper.o
%.o: %.c
gcc -c -Wall -Werror -o $# $<
clean:
rm -f *.o test
To run
$ ./test
Hello world
It's a bit difficult to tell what else might be wrong without the program; my guess is you simply forgot the -c flag to gcc, or forgot to link in helper.o.
undefined reference to 'func_found_in_helper.c'
That's a little odd, as it suggests you have tried to call the function using the '.c' extension, rather than just the function name. Maybe the '.' is just a typo in the question ?
Also a linker will flag an undefined symbol, so it may also be that you have not told the linker where to find helper.o ( the helper.c file compiled to the an object file ). The compiler will start the linker automatically. Did you compile helper.c first ?

What is wrong with my makefile? I get an "undefined reference" error

I'm pretty new to c coding and I've checked the standard references on makefiles but I seem to be misunderstanding something because I'm getting an error. If anybody can help me it would be greatly appreciated.
Here it is:
#makefile
all: runme dontrunme
runme: runme.o
clang runme.o -o runme
dontrunme: dontrunme.o library.o fun.o
clang dontrunme.o -o dontrunme
runme.o: library.o runme.c
clang -c runme.c -o runme.o
dontrunme.o: dontrunme.c
clang -c dontrunme.c -o dontrunme.o
library.o: library.c library.h
clang -c library.c -o library.o
fun.o: fun.c fun.h
clang -c fun.c -o fun.o
clean:
rm -f *.o runme dontrunme
The error I get when I type make is:
runme.o:runme.c:function main: error: undefined reference to 'reverse'
reverse is a function whose protoype is in library.h and is defined in library.c
Any help in understanding where I went wrong is greatly appreciated.
Edit: Here is the code for runme.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "library.h"
int main(int argc, char * argv[]){
char s[] = "Hello World!";
printf("%s\n", s);
reverse(s);
printf("%s\n", s);
}
Change this:
runme: runme.o
clang runme.o -o runme
to this:
runme: runme.o library.o
clang runme.o library.o -o runme
And do the same for dontrunme
dontrunme: dontrunme.o library.o fun.o
clang dontrunme.o library.o fun.o -o dontrunme
In both cases you're not including all the object code files in your final links.
please change order in makefile as follows. you first need library.o for build runme.o
so please build library.o first then runme.o
library.o: library.c library.h
clang -c library.c -o library.o
runme.o: library.o runme.c
clang -c runme.c -o runme.o

undefined reference to `test' when linking shared object

I'm studying a tutorial how to link shared objects in C
Here's my make file
test: glenn.c libhala.so
gcc glenn.c -L. -o test
libhala.so: hala.o
gcc -shared hala.o -o libhala.so
hala.o: hala.c hala.h
gcc -c -Wall -Werror -fpic hala.c
clean:
rm *.o
rm *.so
rm test
hala.h
#ifndef HALA
#define HALA
extern void test(char*);
#endif
hala.c
#include "hala.h"
#include <stdio.h>
extern void test(char* s)
{
printf("%s", s);
}
glenn.c
#include <stdio.h>
#include "hala.h"
int main()
{
test("Hello There!");
return 0;
}
This stocks me up. Help me please..
You should add -lhaha when you link glenn.c.
gcc glenn.c -L. -lhala -o test
Add -lhala while compiling glenn.c, so update makefile as
test: glenn.c libhala.so
gcc glenn.c -L. -lhala -o test

Undefined reference resulting from g++ linking

I am new to g++ and Makefile. I am trying to link this BeBOP SMC library, which is in my lib directory. Under the lib directory are bebop_util and sparse_matrix_converter, both of which have already been built without errors. I see libbebop_util.a, libbebop_util.so under bebop_util and libsparse_matrix_converter.a, libsparse_matrix_converter.so under sparse_matrix_converter. Below is the source:
Makefile
CC=g++
CFLAGS=-c -Wall
test.out: test.o
$(CC) -o test.out -Ilib/sparse_matrix_converter/include -Llib/bebop_util \
-Llib/sparse_matrix_converter -lbebop_util -lsparse_matrix_converter test.o
test.o: test.cpp
$(CC) $(CFLAGS) -Ilib/sparse_matrix_converter/include test.cpp
clean:
rm -f test.o test.out
test.cpp
#include <bebop/smc/sparse_matrix.h>
#include <bebop/smc/sparse_matrix_ops.h>
int main(int argc, const char* argv[])
{
struct sparse_matrix_t* A = load_sparse_matrix (MATRIX_MARKET, "sample_input");
destroy_sparse_matrix(A);
return 0;
}
Output:
login3% make
g++ -c -Wall -Ilib/sparse_matrix_converter/include test.cpp
g++ -o test.out -Ilib/sparse_matrix_converter/include -Llib/bebop_util -Llib/sparse_matrix_converter -lbebop_util -lsparse_matrix_converter test.o
test.o: In function `main':
test.cpp:(.text+0x1a): undefined reference to `load_sparse_matrix(sparse_matrix_file_format_t, char const*)'
test.cpp:(.text+0x27): undefined reference to `destroy_sparse_matrix(sparse_matrix_t*)'
collect2: ld returned 1 exit status
make: *** [test.out] Error 1
Please note that test.cpp depends on sparse_matrix_converter, which depends on bebop_util. Would you please let me know what mistakes I may have made? Thanks.
Tom
The BeBOP code looks to be C code but hasn't add the correct C++ guards. Surround your includes with extern "C" to fix that:
extern "C" {
#include <bebop/smc/sparse_matrix.h>
#include <bebop/smc/sparse_matrix_ops.h>
}

Resources