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,

Recommended Answers

All 2 Replies

>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.

Thanks, nice site by the way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.