LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-25-2003, 10:45 PM   #1
CodeWarrior
Member
 
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, 12:44 AM   #2
stuNNed
Member
 
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, 11:21 AM   #3
jeempc
Member
 
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

main()
{
...
}

Now to complile

gcc myprogram.c myfunctions.c

That should work.
Jeem
 
Old 12-29-2003, 10:27 AM   #4
vasudevadas
Member
 
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, 04:18 AM   #5
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

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

Go here --> http://www.linuxselfhelp.com/gnu/mak.../make_toc.html for learning about makefiles.
 
  


Reply



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 07:49 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 05:25 PM
Using g++ to compile multiple source files arnab_be Linux - Newbie 1 03-03-2004 02:22 PM
Using g++ to compile multiple source files arnab_be Linux - Newbie 1 03-03-2004 02:21 PM
How to delete the destination files while the source files deleted in cp -u ? myunicom Linux - General 4 09-26-2003 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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