5 Replies - 2485 Views - Last Post: 24 September 2009 - 08:31 PM Rate Topic: -----

#1 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

How to compile multiple files together?

Post icon  Posted 24 September 2009 - 07:33 PM

Okay, so I have to make a program, but I have to do it in parts. I need to have the main file that contains the main function, then a file for the header, and another file containing all the functions. I did this everything, but now how do I compile everything together?

Here is what I have so far. I've only dealt with one function for now to keep things simple:

Main file:
#include "function.h"

int main()
{

	random();

	return 0;

}




Function file: (I'm generating a few random numbers)
#include <iostream>
#include <cstdlib>
#include <ctime>

int random();
{

	std::srand(std::time(0));

	const int LEN=3, LOW=1, HIGH=36;

	for(int i=1; i<=LEN; ++i)
		std::cout << (std::rand()%(HIGH-LOW+1)+LOW) << '\n';

	return 0; 

}




Header file:
int random();




Any help, please? :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to compile multiple files together?

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: How to compile multiple files together?

Posted 24 September 2009 - 07:34 PM

What compiler/IDE are you using?
Was This Post Helpful? 0
  • +
  • -

#3 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: How to compile multiple files together?

Posted 24 September 2009 - 07:37 PM

Which compiler are you using?

In most IDEs (intergrated development environments), such as visual c++, code::blocks and dev-c++, you need to create projects and include all files in the project. When you compile these, all files are compiled together.
Was This Post Helpful? 0
  • +
  • -

#4 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: How to compile multiple files together?

Posted 24 September 2009 - 07:39 PM

:crazy: Sorry! Forgot to mention that. I use g ++ in a UNIX environment. Sorry again.
Was This Post Helpful? 0
  • +
  • -

#5 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: How to compile multiple files together?

Posted 24 September 2009 - 08:17 PM

Couldn't I just do the following? Even though I get an error when I do this...

g ++ main.cpp function.cpp header.cpp



Note: I didn't use the actual file names above. I just want a general idea of how to compile multiple files.
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,448
  • Joined: 10-May 07

Re: How to compile multiple files together?

Posted 24 September 2009 - 08:31 PM

You'll need to use dash o to specify the output file.

Or you can make a Makefile that will map the process.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1