Link a .c file with a main.c file - c

I wrote a function for a class and I need to submit it so it works when compiled.
main.c header looks like...
#include <stdio.h>
#include <stdlib.h>
my function code fun.c looks like this...
#include <stdio.h>
#include <stdlib.h>
#include "main.c"
yet for some reason they're not linking together. When I compile and run I don't get an error, it just runs main.c as if fun.c doesn't exist, meaning it ignores the function call to fun.c and runs the program without executing what I need to display.
I wrote the code together in main.c, so I know the function works. I'm just having a little trouble splitting it up into two separate .c files and linking them.
thanks for any help
//edit & updates
compiling with gcc -m32 -O1 *.c the two files in the directory are fun.c and main.c
Basically we're submitting a function that has to work with the main.c that is provided but we're not aloud to modify the main.c so i'm not sure how to add it to my function so they both compile/link together

Please use other names for your outsource files in future, main.c is inconvenient. The right way to do it is: Create a header file for your outsource main.c. It should be named main.h. There you will declare your functions for instance: addNumbers(int a, int b); Then you open your outsource main.c file and there you #include "main.h". Open then your fun.c file and #include "main.h". After everything has been done right, compile your main file: gcc -c -o fun.o fun.c » Then your outsource file: gcc -c -o main.o main.c » Now link the two files together: gcc -o name main.o fun.o Then you are able to execute your program.

Related

GCC error No such file or directory when including a separate file

When I try to compile including the file stack.h my GCC gives me the error "No such file or directory"
gcc -std=c99 -Wall -o Ou1 ou1.c -I C:\Users\NAME\Documents\C programmering\DOA\stack.h
The code looks like this:
#include <stdio.h>
#include <stdbool.h>
#include "stack.h"
I've tried to change its folder and changing the .h to .c and the .c to .h.
First, -I expects a path to a directory. Secondly, your path contains spaces, so you should enclose it in quotes to make sure it's not wrongly treated as two different arguments:
gcc -std=c99 -Wall -o Ou1 ou1.c -I "C:\Users\NAME\Documents\C programmering\DOA"

Why I got "clang: error: cannot specify -o when generating multiple output files"?

I am a newbie in C. I have two simple source code files f1.c and f2.c.
f1.c looks like:
#include <stdio.h>
#include "f.h"
void f1(void) {
// some code ...
}
function f2() in f2.c relies on f1() in f1.c.
#include <stdio.h>
#include "f.h"
void f2(void) {
f1();
}
f1.c and f2.c share a same header f.h,
void f1(void);
void f2(void);
There are no main() access, I just want to compile these two file into a .o file without linker (using -c option),
gcc -c f1.c f2.c -o f2.o
then I got,
clang: error: cannot specify -o when generating multiple output files
but when I mentioned only f2.c, it works well,
gcc -c f2.c -o f2.o
So what's the problem? Thanks!
You should look into the compilation process for C. The first stage is compiling the .c source code into .o object files. The .c files do not need to see the other .c files; they are accepting as fact what you've told them about the existence of external functions. It's not until the linker comes in that it really needs to see the function because the implementation details don't matter to your .c file, just the interface, which you've presumably given it in the header.
What you can do, if you like, is drop the -o flag specifying the output file you want to create. Just compile with
gcc -c f1.c f2.c
and it will know to create f1.o and f2.o which will be able to link against each other when the time comes that you do want to go through with the linking process.
I am curious, however, what your intentions may be for wanting to compile these without linking. I only ask as you refer to yourself as a newbie, so I am wondering if maybe there is an end goal you have in mind and perhaps aren't asking the right question.

Using function from another c file by including its header

I am writing C code (not C++) on a Linux system. I am trying to compile one executable from 2 .c files and 1 header file: main.c, sort.c, and sort.h.
main.c's first line reads: #include "sort.h"
inside sort.h, each function in sort.c is defined like this example:
extern void aFunct(int param);
However, when I try to call a function in sort.c from main.c, I get the following error upon compilation: "undefined reference to 'aFunct'".
If i replace #include "sort.h" with #include "sort.c" my program works without issue. However, as I understand it, this is bad form and I would prefer to avoid this. Thanks for any help.
edit: I am compiling this with a makefile containing the following code:
all: index sort.o
sort.o: sort.c sort.h
gcc -Wall -g -c sort.c
index: main.c sort.o
gcc -Wall -g -o index main.c
clean:
rm index
rm sort.o
edit: I have fixed the problem. The problem did not stem from a misunderstanding of C files and how they link, but rather a misunderstanding of the makefile/gcc commands themself. My program works with the following makefile:
all: index sort.o
sort.o: sort.c
gcc -Wall -g -c sort.c
index: main.c sort.o
gcc -Wall -g -o index main.c sort.o
clean:
rm sort.o
rm index
You should include #include "sort.h" in sort.c as well.
You might be doing this already.
The important point is, you need to make sure that you are building both the .c files [main.c and sort.c]
Now both these obj files [main.o and sort.o] should be the input to linker.
As per my guess, you are not compiling sort.c , so linker is not able to see the implementation of the functions.
When you include one source code file to other (e.g. #include "sort.c") you can have a troubles if you include sort.c file to more than one c-file of the same project. But as I understand you just compile one c-file in which you include sort.c... so it works. But, better read some tutorials, for example How to Create Multi-Module Programs

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"

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