Multiple C Source Files in CLion - c

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;
}

Related

Running another file in C

I'm making a program, and it's supposed to run a file in a different directory (/files/runme.c). How can I run this file in C?
I have tried the system() function, however this does not work.
MAIN.c:
#include <stdio.h>
#include <stdlib.h>
int main() {
system("runme.c");
return 0;
}
runme.c:
#include <stdio.h>
int runme() {
printf("hello world");
}
My expected result is:
hello world
I get:
exit status -1
I want it to run everything inside the contents of runme.c. How can I do this (on Windows and Linux)?
To get the runme() function from a different file to be passed to your main , you need to create a header file with the prototype of the runme() function in it, include this header file in main.c and compile using both files.
main.h:
int runme(void);
main.c
#include <stdio.h>
#include <stdlib.h>
#include "main.h" //main.h needs to be in the same directory as main.c
int main(void) {
runme();
return 0;
}
runme.c
#include <stdio.h>
int runme() {
printf("hello world");
}
Finally compile:
gcc main.c {path}/runme.c
from system() you can run .exe
as far as I know not .c.
Just compile the .c file you want to run put it on same folder as of file containing the main()
eg.
second_program.c->compiled->copy the generated .exe file to the same folder and then use the system(second_program.exe)
or you have to create two file second_program.h for header files and functions prototype etc
and second_program.c for their definition and then use include
if the files are not in the same folder as of main()
then you have to add the location of second_program in your program properties
or add include "second_program.h" if files exist in same folder

How to create a C library with CodeBlocks?

How can I create a C library in CodeBlocks that can be define and used like a standard library with the #include command?
In fact I want to create a simple library that is Composed of several functions.
Basically, you need a .h file for the header definitions and a .c containing the source code.
An example:
/* command.h */
#ifndef COMMAND_H
#define COMMAND_H
int func(void);
#endif /* COMMAND_H */
/* command.c */
#include "command.h"
int func(void)
{
return 0;
}
/* main.c */
#include <stdio.h>
#include "command.h"
int main(void)
{
printf("%d\n", func());
return 0;
}
ifndef is used to prevent the file from being included more than once.
Compile it including both .c files in the command line:
gcc -o demo main.c command.c
Or in your case, follow this guide to compile multiple files in codeblocks.

C - typedef across include files

I'm currently working on a c project and to keep my code organized I use include files. One file includes.h has a typedef and I wan't to access this in a different included file util.h/util.c. I included them both in main.c in the following order: includes.h util.h.
main.c:
#include <stdio.h>
#include <stdlib.h>
//similar includes
#include "includes.h"
#include "util.h"
int main()
{
MyStructType somename;
somename.number = 5;
util_foo(somename);
return 0;
}
includes.h:
#pragma once
struct structname
{
int number;
};
typedef struct structname MyStructType;
util.h:
#pragma once
#include "includes.h" //added line (>accepted answer)
int util_foo(MyStructType blabla);
util.c:
#include <stdio.h>
#include "util.h"
int util_foo(MyStructType blabla)
{
printf("%d\n", blabla.number);
return 0;
}
I compile with this command: gcc -o main *.c
But this doesn't compile, do you have any idea why this doesn't work? Or how to fix it without changing my projects' structure completely?
Edit:
It is recommended to replace #pragma once with:
#ifndef STH_SIMILAR_TO_FILENAME
#define STH_SIMILAR_TO_FILENAME
//code
#endif
You need to add
#include "includes.h"
in util.h in order to make your code compile.
This is because you're relying on transitive #include directives to get the MyStructType typedef into util.h from main.c, but you're not doing the same in util.c.
The best solution (for maintainability purposes) is to minimize relying on transitive inclusions: you should include includes.h wherever it's needed (in this case, in util.h).

including c file includes by header file

I have those 3 files in my workspace:
1.h
#ifndef SOME_HEADER_FILE_H
#define SOME_HEADER_FILE_H
//code
#endif
1.c
#include <stdio.h>
#include "1.h"
//code
main.c
#include "1.h"
int main(){
printf("hello");
}
but in main.c printf is unidentified, is there a way to make it work while the relevant header is called in 1.c?
how can I link the 1.c and 1.h files?
edit: it's an academic assignment and I'm not allowed to make changes in the main and header.
You have included #include <stdio.h> only in 1.c, not in 1.h or main.c.
Obvious solution is to include it in main.c.
Because of the way the #include macro works (it expands the whole header file that you include at the line where you call the macro), you actually don't need to include stdio.h within main.c as long as stdio.h is included in a header file that main.c includes.
Hopefully this makes it clear:
main.c
#include "test.h"
int main()
{
printf("I can call this function thanks to test.h!\n");
return 0;
}
test.h
#include <stdio.h>
This will work just fine.
But this is not the same as being able to use a function that a .c file has access to based on its own #include definition just because you cross-compiled them. In that case the other.c file that calls #include <stdio.h> will get printf(), but main.c does not.
In this setup,
main.c
int main()
{
printf("I don't have any way to call this...\n");
return 0;
}
test.c
#include <stdio.h>
You will not have any way for main to know what printf() is, even if you cross-compile the two. test.c knows what printf() is but not main.
What you want is to have #include <stdio.h> in other.h, and then #include "other.h" in main.c.
But for future reference, this is probably poor practise as it should be immediately apparent what each file's requirements are so that you get a good sense of what its job is.
So here's what I would probably suggest as the best solution:
main.c
#include <stdio.h>
int main()
{
printf("I can call this all on my own.\n");
return 0;
}

How to move C functions into separate file?

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;
}

Resources