I'm getting started with C programming. I currently have a large file that contains a lot of functions. I would like to move these functions to a separate file so that the code is easier to read. However, I can't seem to figure out how to properly include/compile and can't find an example in any online tutorials that I've found. Here's a simplified example:
#include <stdlib.h>
#include <stdio.h>
void func1(void) {
printf("Function 1!\n");
}
void func2(void) {
printf("Function 2!\n");
}
int main(void) {
func1();
func2();
return 0;
}
How do you move C functions into a separate file? FYI: I'm using gcc.
Update: These answers are very helpful, thank you. Now it seems that my simplified example is not good enough because I realized the reason my program failed to compile is because I'm using a global variable in my functions.
#include <stdlib.h>
#include <stdio.h>
int counter = 0;
void func1(void) {
printf("Function 1!\n");
counter++;
}
int main(void) {
func1();
return 0;
}
Moving these functions to an external file doesn't work because they need to reference this global variable:
#include <stdlib.h>
#include <stdio.h>
#include "functions.c"
int counter = 0;
int main(void) {
func1();
counter = 100;
return 0;
}
How can I get around this issue?
Okay. Here we go.
Your main.c file
#include <stdlib.h>
#include <stdio.h>
#include "functions.h"
int main(void) {
func1();
func2();
return 0;
}
Your functions.h file
void func1(void);
void func2(void);
Your functions.c file
#include "functions.h"
void func1(void) {
printf("Function 1!\n");
}
void func2(void) {
printf("Function 2!\n");
}
Compile it with:
gcc -o main.exe main.c functions.c
The most common way is to place your function prototypes in a header file and your function implementations in a source file. For example:
func1.h
#ifndef MY_FUNC1_H
#define MY_FUNC1_H
#include <stdio.h>
// declares a variable
extern int var1;
// declares a function
void func1(void);
#endif
func1.c
#include "func1.h"
// defines a variable
int var1 = 512;
// defines a function
void func1(void) {
printf("Function 1!\n");
}
func2.h:
#ifndef MY_FUNC2_H
#define MY_FUNC2_H
#include <stdio.h>
void func2(void);
#endif
func2.c:
#include "func1.h" // included in order to use var1
#include "func2.h"
void func2(void) {
printf("Function 2 with var1 == %i\n", var1);
}
main.c:
#include <stdio.h>
#include "func1.h"
#include "func2.h"
int main(void) {
var1 += 512;
func1();
func2();
return 0;
}
You would then compile using the following:
gcc -c -o func1.o func1.c
gcc -c -o func2.o func2.c
gcc -c -o main.o main.c
gcc -o myprog main.o func1.o func2.o
./myprog
I only placed one function in each source/header pair for illustration. You could create just one header which includes the prototypes for all of the source files, or you could create multiple header files for each source file. The key is that any source file which will call the function, needs to include a header file which includes the function's prototype.
As a general rule, you only want a header file included once, this is the purpose of the #ifndef #define #endif macros in the header files.
First you have to learn the difference between a declaration and definition. A declaration tells the compiler that something, like a function, exists. A definition is, for the case of functions, the actual function implementation.
So what you do is move the definition to another file, but add a declaration in the file where the function is to be called. You then build both files together, and the compiler and linker will take care of the rest.
You can do something like this.
/* func1.c */
void func1(void) {
printf("Function 1!\n");
}
/* func2.c */
void func2(void) {
printf("Function 2!\n");
}
/* main.c */
#include "func1.c"
#include "func2.c"
int main ( void )
{
func1();
func2();
return 0;
}
Related
In a CLion project, I have two C-language source files, "main.c" and "list.c".
The source file "main.c" has this:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
The source file "list.c" has this:
#include <stdio.h>
int printFoo() {
printf("I want Krabby Patties!\n");
return 0;
}
Now how do I call printFoo() from the main() function? I know I cannot do an include<list.c> in main.c since that will cause a multiple definitions error.
CLion uses CMake for organizing and building project.
CMakeLists.txt contains instructions for building.
Command add_executable(program main.c list.c) creates executable with files main.c and list.c. Add all source files to it. You can add headers, but it isn't necessary.
Header files contain definitions of functions and other things, source files for realization, but you can merge them.
main.c
#include "list.h"
int main() {
printFoo();
return 0;
}
list.h
#pragma once
int printFoo();
list.c
#include "list.h"
#include <stdio.h>
int printFoo(){
return printf("I want Krabby Patties!\n");
}
#pragma once tels compiler to include header file once. If you have more than one include of one file without #pragma once, you'll catch an error.
You can create one header file "list.h"
#ifndef __LIST_H__
#define __LIST_H__
int printFoo();
#endif
Then include it in main.c:
#include <stdio.h>
#include "list.h"
int main() {
printf("Hello, World!\n");
printFoo();
return 0;
}
I need to generate a static library MyLib.lib that contains unimplemented functions.
Inside the project, I called the unimplemented function as shown below:
/* Inside MyLib.c */
#include "MyLib.h"
void foo(void)
{
func(); // To be implemented by the user.
}
And In the header file MyLib.h, I included a header file
#include "user.h" // contains user_imlplementation_of_func()
...
#define func() user_imlplementation_of_func()
To make things simple, let's just give an example of the user.c:
void user_imlplementation_of_func(void)
{
printf("OK");
}
I would like to know is it possible to do this call? Otherwise, Is there any other solution to use unimplemented functions inside a static library and let the user define them after compressing the project from a source code to a .lib file
You need to declare the unimplemented function as extern in the header of your library. This tells the compiler, that the function will be defined somewhere else.
Example:
MyLib.h
void foo();
extern void func();
MyLib.c
#include "MyLib.h"
void foo(void)
{
func();
}
main.c
#include <stdio.h>
#include "MyLib.h"
void func()
{
printf("Hello, world!\n");
}
int main()
{
foo();
}
Example build:
cc -o MyLib.o -c MyLib.c
cc -o out main.c MyLib.o
Output:
$ ./out
Hello, world!
However, for more readability I suggest you to pass your project functions as pointers to your library functions. This is commonly known as callback.
Example:
/* MyLib.c */
#include "MyLib.h"
void foo(void (*func)(void))
{
func();
}
Now you can call the foo function in your project with:
foo(&user_imlplementation_of_func);
Edit:
As stated in the comments by the user theSealion a third solution is the usage of weak symbols. The wikipedia articel "Weak symbol" provides a good explanation with examples.
You can do some else - function pointer:
//youLib.c
extern void (*func)(void) = NULL;
extern void foo()
{
if (func != NULL) {
func();
}
}
//main.c
static void user_imlplementation_of_func(void)
{
printf("Hello\n");
}
func = &user_imlplementation_of_func;
foo(); //Will printf "Hello"
I'm a beginner into Linking, lets say I have two .c files
file1.c is
#include <stdio.h>
#include "file2.c"
int main(int argc, char *argv[])
{
int a = function2();
printf("%d",a);
return 0;
}
and file2.c is
int function2()
{
return 2018;
}
when I compiled, there is a linker error which is multiple definition of function2, but I only define function once in file2.c?
You should create a header file, "file2.h", with:
int function2(void);
and a file "file2.c" with the function: "file2.h" with:
#include "file2.h"
int function2(void)
{
return 2018;
...
}
Then in your main you have to include the header with:
#include "file2.h"
Keep care that all those files should be in the same folder to avoid any link problem
Try something like this:
#include <stdio.h>
#include "file2.h"
int main(int argc, char *argv[])
{
int a = function2();
printf("%d",a);
return 0;
}
file2.h:
extern int function2(void);
and file2.c is
#include "file2.h"
int function2(void)
{
return 2018;
}
And then link it together.
The statement #include "file2.c" effectively incorporates the contents of file2.c into file1.c. Then file1.c is compiled as if it contains:
int function2()
{
return 2018;
}
Those lines define function2; they tell the compiler “Here is function2, create code for it.” Because those lines effectively appear in both file1.c and file2.c, your program has two copies of function2.
Instead, you should create file2.h that contains:
int function2();
That line tells the compiler “There exists a function called function2, but its definition is somewhere else.”
Then, in file1.c, use #include "file2.h" instead of #include "file2.c". This will tell the compiler, while file1.c is being compiled, what it needs to know to compile a call to function2. The compiler will have the declaration it needs, but it will not have the definition, which is not needed in file1.c.
Also, in file2.c, insert #include "file2.h". Then file2.c will contain both a declaration of function2 (from file2.h) and a definition of function2 (from the actual lines in file2.c). The purpose of this is so the compiler can see both the declaration and the definition while it is compiling file2.c, so it can warn you if there is a typographical error that makes them incompatible.
Additionally, in C, you should use int function2(void) rather than int function2(). For historic reasons, the latter leaves the parameters unspecified. Using (void) tells the compiler there are no parameters.
Given 2 files, for examples:
file1.c :
int main(){
f();
return 0;
}
file2.c:
void f(){
return;
}
Why I can't call f from file1.c like that?
Because first you need to tell the compiler (declare) that it exists somewhere:
void f(); //function declaration
int main()
{
f();
return 0;
}
Usually, though, it is better to put such declarations in a separate header file (e.g. file2.h) so that later you could include this file (e.g. #include "file2.h") instead of duplicating such declaration in every other file where you need this function.
The problem is that file1.c does not "know" that the function f exists. You need to use a prototype. The standard way is to put prototypes in header files and definitions in .c files.
It could look like this:
file1.c:
#include "file2.h"
int main(){
f();
return 0;
}
file2.h:
#ifndef FILE2_H
#define FILE2_H
void f();
#endif
file2.c:
#include "file2.h"
void f(){
return;
}
I try to make program in C and I cant use functions from .h without including .c file too. If I include .c after including .h it works. I get "undefined reference to ..." error on every function defined in .h.
main.c:
#include "mp.h"
//#include "mp.c"
int main()
{
int n;
printf("Unesite broj clanova niza: ");
scanf("%d",&n);
int *a=(int *)malloc(n*sizeof(int));
if (a==NULL) exit(0);
unos(a,n);
sortiranje(a,n,rastuci);
stampanje(a,n);
sortiranje(a,n,opadajuci);
stampanje(a,n);
return 0;
}
mp.h:
#ifndef MP_H_INCLUDED
#define MP_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
enum tip_sort {opadajuci,rastuci};
void unos(int *, int);
void sortiranje(int *, int, enum tip_sort);
void stampanje(int *, int);
#endif // MP_H_INCLUDED
mp.c:
#include "mp.h"
void unos(int *a, int n){
...
}
void sortiranje(int *a, int n, enum tip_sort t){
...
}
void stampanje(int *a, int n){
...
}
What you're seeing is a linker error. I guess, you're trying to compile main.c all alone.
You compilation statement should look like
gcc main.c mp.c -o output
and yes, do not #include .c (source) files. Source files are meant to be compiled and linked together to form the binary.
Note: Also, please do not cast the return value of malloc().