Using a static library in c (.a file) - c

I am trying to create a static library using a shell script.
For creating the library I used following script:
gcc -c -Wall -Wextra -Werror *.c
ar -cr libft.a *.o
There are 5 different functions in 5 .c files.
Now I wanted to test the library and created a main.c file with this content:
#include "libft.a"
int main(void)
{
ft_putstr("hello");
}
Compiling the main.c (with gcc main.c) returns 419 warning and at least 20 errors looking something like this:
./libft.a:4:492: warning: null character ignored [-Wnull-character]
...<U+0000>UH<89><E5>H<83><EC><U+0010>#<88>}<FF><BF><U+0001><U+0000><U+0000><U+0000>H<8D>u...
Before this I was working with .h files which worked fine but this time I wasn't supposed to create a .h file so I don't what to do know.

#include "file_name" means like "please copy and paste the contents of the file file_name here", so it shouldn't be used with .a file.
You can write the declaration of the function to use directly instead of using .h file.
/* guessed from usage, please use correct argument and return type */
void ft_putstr(const char*);
int main(void)
{
ft_putstr("hello");
}
Then compile the source code with linking with the library:
gcc main.c -lft

When you compile your main.c, add the library name to it like this:
gcc main.c libft.a

Related

Makefile with two .c files and no header

Trying to write a makefile with two .c files and no header. However, it seems examples online have shown the makefile with a header.
So i tried to write a header file and it would tell me a function is redefined somewhere else. my header consisted of declarations of functions in both my .c files.
#ifndef HEADER_H
#define HEADER_H
void calc(void parameters);
int main(int argc, char* argv[]);
struct compArray
{
int start;
int end;
int thr;
int m;
int r;
};
#endif
I'm positive that's not how you write a header but ideally I'd like to have my makefile without a header. Below is my Makefile:
all: thr_atomic.o thr_reduce.o
gcc -o make thr_atomic.o thr_reduce.o -lm -pthread
thr_atomic.o: thr_atomic.c
gcc -c thr_atomic.c
thr_reduce.o: thr_reduce.c
gcc -c thr_reduce.c
Is it possible to create a makefile without a header? My two .c files are independent of each other. If so how do I go about doing that? And if not, what would I put in my header.h to tell the computer that my variables are not being redefined and are independent from each other?
You can use the following Makefile:
all: thr_atomic thr_reduce
thr_atomic: thr_atomic.c
gcc thr_atomic.c -lm -pthread
thr_reduce: thr_reduce.c
gcc thr_reduce.c -lm -pthread <newline>
However, since it seems the two programs are totally independent, I would rather make two separate makefiles.
From what I can understand from your question, you don't need a header file in order to compile C code using a Makefile. Makefiles don't have anything to do with source code. All a Makefile does is run a command(s) if the file(s) you've specified have been updated.
# Makefile
outfile: infile1 infile2
command1
command2
# ^ commands MUST begin with a tab
If you run make outfile, this is what happens:
If outfile does not exist, then make runs command1, and then runs command2.
If outfile exists, but infile1 and infile2 have not been updated since then, make will do nothing.
If outfile exists and infile1 or infile2 have been updated since outfile was last modified, then make will run command1 and then run command2.
That's the basics of make and Makefiles. The files (outfile, infile1, infile2 etc.) and the commands (command1, command2) can be anything you like. There can also be any number of them present.
When it comes to compiling code, make is pretty useful because you can use it to recompile a binary file (also called an executable file) if the source files are changed, which is exactly what you want.
For example, this makefile creates a binary file (executable file) from two source files (and no header files):
# Makefile
# executable file is called "prog"
prog: a.o b.o
gcc -o prog a.o b.o
# ^^^^^^^
# tell gcc to name the output file "prog"
# compile the first source file "a.c" to produce the object file "a.o"
a.o: a.c
gcc -c a.c
# compile the second source file "b.c" to produce the object file "b.o"
b.o: b.c
gcc -c b.c
If you do have header files, then you would need to change the compilation lines to this:
# Makefile
# ...
# compile a.c, which has an associated header file a.h
a.o: a.c a.h
# ^^^ add the header file here
gcc -c a.c
If you are not clear about what header files are, then look them up somewhere or ask another question about them.

How to use gcc correctly from the ubuntu terminal?

I'm try to compile a .c file from the terminal using gcc. The file includes a personal library where a function is defined. This library.h and the .c file are in the same directory.
I get the following message error
undefined reference to `function'"
Should I use another argument as:
gcc -o nameoutput filename
or should I place the library.h in another directory?
Assuming you have library.c, library.h and main.c in your current working directory:
$ gcc -Wall main.c library.c -o my_program
and then to run it:
$ ./my_program
"Undefined reference" means that the linker can't find the object file containing the compiled body of function; it doesn't have anything to do with the .h file.
I sounds like you have a situation where library.h and library.c are in one directory, and main.c is in a different directory. If that's the case, then your command line will need to look something like this:
gcc -o program -I /path/to/library main.c /path/to/library/library.c
-I /path/to/library means that gcc will look for .h files in that path as well as the standard include paths. That also allows you to write
#include "library.h"
instead of
#include "/path/to/library/library.h"
in any code that needs it.

C include error multiple definition error

I'm encountering a classic error but still don't get why it occurs...
Below is the simplified explanation
Apparently I have two C files main.c and support.c
in support.c i have one function void bla(int input);
in main.c i have several functions using bla from support.c, and i included
#include<support.c>
at the top of main.c
However I cannot compile the project because of the error multiple definition of bla, first defined here (eclipse points to the definition of bla in support.c)
I know that optimally I would have to create header file support.h and gives prototype extern void bla(int input) there, but for this I have to include the .c file.
Thanks in advance.
The preprocessor will copy the contents of support.c, and paste it to main.c to replace the line #include<support.c>. So there are two definition of the function bla, one in support.c, the other in main.c.
The solution is, don't include an source file. Put the declarations of functions that you want to export in a header file support.h, and include the header file in main.c:
#include "support.h"
You don't include source files into other source files. Instead you make a header file (with the extension .h) that contains declarations of functions, i.e. function prototypes. Then you build both source files separately, and link them together to form the final executable.
So a header file support.h like
#ifndef SUPPORT_H
#define SUPPORT_H
void blah(void);
#endif
(The preprocessor #ifdef/#define/#endif things are for include guards, to protect from multiple inclusion in the same source file.)
Then the support.c source file
#include "support.h"
void blah(void)
{
/* Some code here... */
}
And finally the main.c source file
#include "support.h"
int main(void)
{
blah();
return 0;
}
If you have an IDE (like Visual Studio) if you add these files to your project the IDE will make sure everything is built and linked properly. If you're compiling on the command line, compile each source file into an object file (usually using an option like -c (used for GCC and clang)) and then link the two object files together to create the executable.
Command line example with GCC:
$ gcc -Wall -c main.c -o main.o
$ gcc -Wall -c support.c -o support.o
$ gcc main.o support.o -o my_program
The above three commands will first compile the source files into object files, and then link them together.
What compiler are you using?
When compiling, make sure you do this:
gcc main.c support.c -o yourProgram

Undefined reference to functions C

I downloaded a library from here. I added the header file in my program but when I try to access the functions of this header file, I get error:
undefined reference to the function for u_open() and u_accept(). I tried to compile the .c files of this header file but I get this error:
undefined reference to main.
I tried everything in my knowledge to solve this issue, but couldn't solve it. Here is the program.
#include "uici.h"
int main()
{
char client[50];
char buf[1024];
u_port_t portnumber;
portnumber=48625;
int fd = u_open(portnumber);
int communFd = u_accept(fd,client,50);
perror("Opened");
fprintf(stderr,"\nComun fd is %d\n\n\n",communFd);
read(communFd,buf,1024);
write(STDOUT_FILENO,buf,1024);
fprintf(stderr,"\n\nReading complete\n");
return 0;
}
What can I do to solve this problem?
Regards
Your header file uici.h declares the functions you're calling in main() (u_open() and u_accept()), but not their implementations, which are in the source files. When the linker tries to create the entire program, it then complains that the implementations can't be found.
The solution is to link all the files together when creating the actual program binary. When using the g++ frontend, you can do this by specifying all the source files together on the command line. For example:
g++ -o main main.c uici.c
will create the main program called "main", assuming that the implementations you need are in uici.c.
edit: In the case you're linking against a prebuilt library for uici, you'll need to specify to the frontend that the library is needed for linking, e.g. with:
g++ -o main main.c -luici
You need to link the library when using gcc like :
gcc nameofprgram.c -l<library>
Use any of these flags while compiling with gcc
-I <searchpath to include files>
-L <searchpath to the lib file>
-l<thelibname>
Ex:
gcc -o myprogram -lfoo -L/home/me/foo/lib myprogram.c
This will link myprogram with the static library libfoo.a in the folder /home/me/foo/lib

C header files and dynamic linking error

I have created a dynamically linked library. The only problem I have is that my main program does not recognize my header file. The header file is in a separate folder from my main program. I have tried #include "myheader.h" as well as #include "/folder/dir/myheader.h"
Here is what my .h consist of
extern int afunction(int,int);
extern int afunction(int,int);
So far this code works
gcc -fPIC -c filename1.c
gcc -fPIC -c filename2.c
gcc -shared -o libMylib.so filename1.o filename2.o
I then copy the lib to /usr/local/lib, and then
gcc main.c -L. -lMylib -o exeName -ldl
and I get
" myheader.h : no such file or directory.
Here is my directory structure:
directory1 ----------------folder1(main program.c)
directory1 ----------------folder2(myheader.h, along with functions for the pgm)
A push in the right direction would help, as I have written all my code and I am just at the last phase.
You need gcc ... -I<some directory to myheader.h>. That will enable the compiler to find the header file.
You can put your lib header files in the same folder with your current program.
or like #Ed Heal said.. adding -I<path> for include header folder.

Resources