Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-25-2003, 11:45 PM   #1
Registered: Mar 2003
Location: US
Distribution: Kubuntu 6.06
Posts: 173

Rep: Reputation: 30
I need help using multiple source files

Most of the C/C++ programs I have written contain all my variables and functions in one file with a main(). I want to learn how to split my code between multiple files, but it seems that I have never been showed how to do this. The main reason I want to do this is because I want to be able to put functions that I commonly use into a file and compile it with the file that contains main(). Does anyone have a tutorial that shows how to do this? Only one fiel can contain the function main(), correct? If this is so how do these other source files look?
Old 12-26-2003, 01:44 AM   #2
Registered: Aug 2003
Distribution: Gentoo/Ubuntu
Posts: 134

Rep: Reputation: 15
newbie response:: you can create other functions as files and even functions within functions you just need to call them after they are defined or something. bash handles this quite well.
Old 12-26-2003, 12:21 PM   #3
Registered: Mar 2003
Location: Ann Arbor, MI
Distribution: Slackware 10.1
Posts: 119

Rep: Reputation: 15
Here is one way.
Put your function definations in a file called myfunctions.c . Or whatever name you want. Put the function declarations in a file called myfunctions.h
In main do this.

# myprogram.c
#include <stdio.h> // or whatever you use
#include "myfunctions.h" // put this file in the same directory as main


Now to complile

gcc myprogram.c myfunctions.c

That should work.
Old 12-29-2003, 11:27 AM   #4
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
The long-winded way to do it, as per jeempc's example, is to first do:

gcc -c myfunctions.c
gcc -c myprogram.c

That should create myfunctions.o and myprogram.o. Then do:

gcc -o myprogram myfunctions.o myprogram.o

which will create myprogram, an executable file. If you're entering the gcc command by hand then you'll want to do what jeempc told you. If you were to write a makefile to automate your compilation (you'll want to do this if your project included many files) then you'd want to do it my way.
Old 12-30-2003, 05:18 AM   #5
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 31
I think the solution is makefiles. It will help you automate your compilation from among multiple source file.

Go here --> for learning about makefiles.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple C source files howto introuble Programming 2 07-31-2005 08:49 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 06:25 PM
Using g++ to compile multiple source files arnab_be Linux - Newbie 1 03-03-2004 03:22 PM
Using g++ to compile multiple source files arnab_be Linux - Newbie 1 03-03-2004 03:21 PM
How to delete the destination files while the source files deleted in cp -u ? myunicom Linux - General 4 09-26-2003 02:13 PM

All times are GMT -5. The time now is 05:01 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration