undefined reference to `test' when linking shared object - c

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

Related

How to execute a certain part using makefile in C (LINUX)

I am developing a C program that calls 2 functions, func1() and func2().
Here is the code
void func1(void){/*...*/}
void func2(void){/*...*/}
int main(){
func1();
func2();
return 0;
}
Is there a way that when I compile the program using make and a certain flag, it just executes func1() and if I compile using another flag then it only executes func2()?
How to make such a makefile? Can anyone provide me with a makefile that does this job?
You can use the -D flag of the compiler:
cc -o demo demo.c -Dfunc=func1
then
#include <stdio.h>
void func1(void) { puts("1"); }
void func2(void) { puts("2"); }
int main(void)
{
func();
return 0;
}
Output:
1
You can also pass the function as an argument to make:
CC = gcc
CFLAGS = -std=c11 -Wpedantic -Wall
action: demo.c
$(CC) $(CFLAGS) demo.c -o demo -Dfunc=$(argument)
clean:
rm -f demo
call make using:
make action argument=func1
or
make action argument=func2
But if you really want to do that:
int main(){
func1();
func2();
call both functions inside main, you should use another approach (although this solution seems quite sticky to me):
#include <stdio.h>
void func0(void) { /* do nothing */ }
void func1(void) { puts("1"); }
void func2(void) { puts("2"); }
#ifdef delete_func1
#define func1 func0
#endif
#ifdef delete_func2
#define func2 func0
#endif
int main(void)
{
func1();
func2();
return 0;
}
compile using:
cc -o demo demo.c -Ddelete_func1
or
cc -o demo demo.c -Ddelete_func2
then the Makefile should look like:
CC = gcc
CFLAGS = -std=c11 -Wpedantic -Wall
func1: demo.c
$(CC) $(CFLAGS) demo.c -o demo -Ddelete_func2
func2: demo.c
$(CC) $(CFLAGS) demo.c -o demo -Ddelete_func1
clean:
rm -f demo
call it using:
make func1
or
make func2
you can delete both functions using -D twice:
cc -o demo demo.c -Ddelete_func1 -Ddelete_func2

Linking extern variables using multiple library in C

In my project, I have two libraries and one program.
Lib1.c and Lib1.h are two files of first library(Lib1.so).
Lib2.c and Lib2.h are two files of second library(Lib2.so).
prog.c is the main file of program(prog).
The program(prog) is linked only to the second library(Lib2.so) and the second library(Lib2.so) is linked to the first library(Lib1.so).
In Lib1.c, I have a declaration of global variable (int var = 0;) and in Lib1.h, I have a declaration (extern int var;).
In Lib2.h, I have a declaration (extern int var;) in order to use var variable in main program.
In main() function, I include the Lib2.h in prog.c file and I have a declaration (var = 5;)
Lib1.c :
#include <stdio.h>
#include "Lib1.h"
int var = 0;
int funct(void)
{
printf("hello world \n");
return 0;
}
Lib1.h :
extern int var;
int funct(void);
Lib2.c :
#include <stdio.h>
#include "Lib2.h"
int funct2(void)
{
printf("Library 2 \n");
funct();
return 0;
}
Lib2.h :
#include "Lib1.h"
extern int var;
int funct2(void);
prog.c :
#include <stdio.h>
#include "Lib2.h"
int main()
{
var = 5;
printf("===>var=%d\n", var);
funct2();
return 1;
}
Commands :
gcc -c -Wall -Werror -fpic Lib1.c
gcc -shared -o Lib1.so Lib1.o
gcc -c -Wall -Werror -fpic Lib2.c
gcc -shared -o Lib2.so Lib2.o -ldl /home/test/Lib1.so
gcc prog.c -o prog -ldl /home/test/Lib2.so
When I try to compile the program(prog.c), I get an error in the link step as below.
/usr/bin/ld: /tmp/ccKaq16a.o: undefined reference to symbol 'var'
/home/test/Lib1.so: error adding symbols: DSO missing from command line
Is there a way to use var variable in the main function when its defined in the first library?
You link your program against Lib2 but not Lib1. You need to add that as well. You also don't need to explicitly link Lib1 when you create Lib2
gcc -c -Wall -Werror -fpic Lib1.c
gcc -shared -o Lib1.so Lib1.o
gcc -c -Wall -Werror -fpic Lib2.c
gcc -shared -o Lib2.so Lib2.o
gcc prog.c -o prog /home/test/Lib2.so /home/test/Lib1.so

undefined referance to function included in header

I would apreciate any insights to a couple of linker and compiler issues I have
I have main.c file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "assemble.h"
char *fileName;
FILE *file;
int main(int argc, char *argv[]){
char inputFile[MAX_INPUT];
int i, flag = TRUE;
for(i=1; i<argc; i++){
fileName = argv[i];
strcpy(inputFile,argv[i]);
file = fopen(inputFile,"r");
}
flag = assemble(file, fileName); //****this is the problam line****
if(!flag)
printf("Errors found, compilation aborted\n");
else
printf("File %s compiled\n", fileName);
fclose(file);
return 0;
}
In the header assemble.h I have the decleration:
int assemble(FILE *file, char *fileName);
The first issue is in main.c I get undefined referance to 'assemble', id returned 1 exit status
The second issue is in assemble.h I get FILE was not declared in this scope.
Does anyone know what causes these errors and what is the fix?
Thanks in advance
[EDIT]: the makefile.
assembler: main.o assemble.o functions.o
gcc -Wall -ansi -pedantic main.o assemble.o functions.o -o assembler -lm
main.o: main.c assemble.c
gcc -Wall -ansi -pedantic -c main.c assemble.c -lm
assemble.o: assemble.c
gcc -Wall -ansi -pedantic -c assemble.c -lm
functions.o: functions.c
gcc -Wall -ansi -pedantic -c functions.c -lm
clean:
rm -f assembler
rm -f *.o
You need to compile both files and link them together.
gcc main.c assemble.c
Or compile them separately to object files and link them.
gcc -c main.c
gcc -c assemble.c
gcc main.o assemble.o

Undefined reference to 'readline' - 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.

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 ?

Resources