473,326 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Help funtions in a seperate file??

I have made a lot of helping functions that I would like to have in a
seperate file. I have tried putting them in a file "functions.c". I then
have the file "myprogram.c" that contains some structs and other primary
functions.

In my make file I then have:

myprogram: myprogram.c functions.c
gcc myprogram.c -o myprogram

But that does not work. Is it really impossible to put functions in other
files?

Nov 15 '05 #1
3 1322
Paminu wrote:
I have made a lot of helping functions that I would like to have in a
seperate file. I have tried putting them in a file "functions.c". I then
have the file "myprogram.c" that contains some structs and other primary
functions.

In my make file I then have:

myprogram: myprogram.c functions.c
gcc myprogram.c -o myprogram

But that does not work. Is it really impossible to put functions in other
files?

No, but you need to declare them, and the object files must be linked
simultaneously.

functions.h:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void foo(void);
void bar(void);
....
#endif

functions.c:
#include "functions.h"

void foo(void) {
....
}

void bar(void) {
....
}

myprogram.c:
#include "functions.h"

int main(void) {
foo();
bar();
}

Makefile:
myprogram: myprogram.o functions.o
gcc -o myprogram myprogram.o functions.o
myprogram.o: functions.h myprogram.c
gcc -c myprogram.c
functions.o: functions.h functions.c
gcc -c functions.c

The makefile sketched here is a bit silly since it makes everything
explicit (and doesn't use variables or implicit rules), but writing
makefiles effectively is another chapter.

Lastly, "functions.c" is an awful name for a unit because almost any
unit will contain functions. It's a much better idea to split up the
program according to areas of functionality, and think of more
descriptive names. If that means creating a unit with only one function
(foo.c, bar.c...), it's still better than producing a dump.

S.
Nov 15 '05 #2
On Sun, 16 Oct 2005 19:39:27 +0200, Paminu wrote:
I have made a lot of helping functions that I would like to have in a
seperate file. I have tried putting them in a file "functions.c". I then
have the file "myprogram.c" that contains some structs and other primary
functions.

In my make file I then have:

myprogram: myprogram.c functions.c
gcc myprogram.c -o myprogram

But that does not work. Is it really impossible to put functions in other
files?


Certainly. A Makefile is interpreted by the make command and spawns
commands (e.g. gcc) in a local shell. In your example, the spawned gcc
program isn't provided with any arguments regarding functions.c. So you
just need to add functions.c to the gcc line like:

myprogram: myprogram.c functions.c
gcc functions.c myprogram.c -o myprogram

Mike

Nov 15 '05 #3
"Paminu" <ja******@asd.com> wrote

In my make file I then have:

myprogram: myprogram.c functions.c
gcc myprogram.c -o myprogram

But that does not work. Is it really impossible to put functions in other
files?

It is certainly possible. Most real C programs contain more than one source
file.

type
gcc myprogram.c functions.c -lm

to compile a multi-function program. -lm links the maths library, which you
may not be using.

make is a horrible tool with strange rules and requirements for tabs etc.
Once you have the program working, you need to fiddle with make's arcane
rules to get it to do what you want. If you look at someone else's makefile
and use that as a skeleton it will probably be the easiest thing.

By splitting up the actions, you find out where the problem lies.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: AJ | last post by:
Hi All I'm pretty new to PHP. I've done some relatively complicated bits now: written my own little shopping cart, written a content management system with file uploads, built in newsletters,...
19
by: Kenneth Lantrip | last post by:
trying to make something like this work... #include <stdio.h> int main (void) { #if defined UNIX printf("\n\n\n%s\n\n", "Program is running on a flavor of Unix."); #endif
7
by: Jon S via DotNetMonster.com | last post by:
Hi all, I posted this yesterday but didnt explain it too well so thought I would do it again. I have a label on a form that I want to be able to change the text while I'm in a seperate class...
5
by: Willson | last post by:
Ok, first off I am learning C++ for the first time. I know Java very well, but a new class of mine requires C++. So bare with me please. I have 2 classes, Birthday.cpp and BirthdayParadox.cpp ...
3
by: james121285 | last post by:
I have been trying this program for ages and am not getting very far. I am trying to input data from a seperate file and use it to work out the max and min values of the data. I have done the second...
8
by: gianluca | last post by:
Hy, I need help about use dll with ctypes. I've compiled my dll C library and I copied it in c:\windows\system32. When I load it with "myDLL=cdll.LoadLibrary(find_library("myDLL.dll"))" It seem...
2
by: Baldaris | last post by:
Hi I am using Xampp , Php 5.2.6 and phpedit for wiriting code. i am trying to add the content's from user form and then add then into the data base. It check's if submit is clicked or not ,...
3
by: akshaycjoshi | last post by:
IT"S A LONG QUESTION.PLEASE BE PATIENT TO READ IT. I HAVE EXPLAINED IT TO MY MAXIMUM CAPACITY. I am making one app. in which i need to create a seperate access database for each day. Its a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.