0

How would I reuse functions in C? I've tried simply sticking them in a #include <myfunctions.h> , but that didn't work... Any (complete) suggestions/links on how to do this? I googled some and found this:

The way to write a header file is:

functions.h:
int sum( int a, int b) ;

functions.cpp:
#include "functions.h"
int sum( int a, int b) {
return a+b ;
}

compile:
g++ -c functions.cpp -o functions.o

main.cpp:
#include "functions.h"
main() {
cout << sum(1,2) << endl ;
}

compile and execute main:
g++ main.cpp functions.o -o main
main

How would I, obviously replacing g++ with gcc and *.cpp with *.c, do these steps in Code::Blocks?

Thanks in advance,

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Clockowl
0

>How would I reuse functions in C?
The easiest way is to create a header file with the declarations and link to a .c file with the definitions. Download one of the libraries here for a good example of that technique. Using Code::Blocks all you have to do is add the two files to your project and build.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.