How to use gcc correctly from the ubuntu terminal? - c

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.

Related

Using a static library in c (.a file)

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

In C how do I compile and then create an executable file with a header and two c files?

I have three C files in total. One is a header [.h] file, two are source [.c] files.
The .h file is called encryption.h and the corresponding source file is encryption.c. The encryption.c has logic, but no main() function. My second c file is called main.c. There I have the main() function that calls methods from encryption.c.
I am compiling these files within terminal on Mac OSx. I am confused on how to compile this, I have tried the following:
gcc -c main.c
gcc -c encryption.c
gcc -c encryption.h
gcc main.o encryption.o encryption.g.gch -o encrypt
This doesn't seem to work though, it says I have a precompiled-header already. I tried finding the answer online, I know it has to be simple, but I haven't had much luck. What is the issue here?
Don't compile the header file. Header files are meant to be included to the source files (using #include directive, in c). Just compile the source files and link them together. Something like
gcc -c main.c
gcc -c encryption.c
gcc main.o encryption.o -o encrypt
or, for shorthand,
gcc main.c encryption.c -o encrypt
Note: If you're bothered about the presence (or absence) of header files while compilation, check the pre-processed output of each source files using gcc -E option.

header.h: No such file or directory even though source and header are in same directory

I have made a header and a source but I don't know how to link them up. I looked it up on the web but the commands provided didn't work (or I wouldn't be here :) ).
To compile it (if you use GCC):
Header:
$ gcc -c whatever.h -o whatever.o
Source:
$ gcc -c sample.c -o sample.o
To link the files to create an executable file:
$ gcc sample.o whatever.o -o sample
What did I do wrong. I am using geany for writing (compile error is here) but the commands are executed on a terminal in the same directory. can anybody give me the build commands for geany so whenever I want to include a header I can just compile and run?
Good and the right way would be to
sample.c
#include "header.h"
and compile
gcc sample.c -o ob
Thumb Rule:
header files [.h] are for #includeing
source files [.c] are for compiling and linking together to create the executable.
Once you've #included your header file in a .c file, there's no need to compile the header file and produce an object file.
FYI, you can check the effect of #include-ing the header file by running
gcc -E sample.c
and hope you'll understand why you need not compile and link the header file separately.
EDIT:
if you have a sample.c and whatever.h, to produce and run the binary, simply do
#include "whatever.h" in the top of sample.c
gcc -o sample sample.c
./sample
if you include header file by:
#include <header.h>
it will give this error.
Instead you can write as given below:
#include "header.h"

GCC unable to find header file in a included library

I am trying to include a library file named libmathematica.a in gcc so it gets linked in the executable example.
I attempt to do this with gcc main.c libmathematica.a -o example
Note: I have to do this with gcc, as ld won't link it properly with the correct system libraries
But I get: fatal error: mathematica.h: No such file or directory ,which is odd because mathematica.h is in the library.
Can you help?
A header file cannot be in the library. It has to be present at a certain location and you have to specify that location with the -I compiler flag:
gcc -I/path/to/mathematica/include main.c libmathematica.a -o example
If the header file is in the directory where the main.c is or in a subdirectory, then be sure that you use quotes and not angle brackets in the #include directive.
The issue would be in your source file. If the mathematica.h is in the system includes directory then you would use #include <mathematica.h> and if it was in some local directory then you would use something like #include "libs/mathematica.h".
Try adding to the gcc call - an option like
-I/Full/Path/To/The/Directory/Where/the/desired/header/resides
For example:
gcc -I/usr/include/mathematica -lmathematica -o example main.c

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