# How to compile multiple files together?

Page 1 of 1

## 5 Replies - 6258 Views - Last Post: 24 September 2009 - 08:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=127935&amp;s=8e2042b786c4155e9505dc2124ef7ddf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nautica17

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

# How to compile multiple files together?

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;

}

```

```int random();

```

Is This A Good Question/Topic? 0

## Replies To: How to compile multiple files together?

### #2 eker676

• Software Engineer

Reputation: 379
• 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?

### #3 Louisda16th

• dream.in.assembly.code

Reputation: 15
• 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.

### #4 nautica17

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

## Re: How to compile multiple files together?

Posted 24 September 2009 - 07:39 PM

Sorry! Forgot to mention that. I use g ++ in a UNIX environment. Sorry again.

### #5 nautica17

Reputation: 6
• 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.

### #6 no2pencil

• Professor Snuggly Pants

Reputation: 6635
• Posts: 30,898
• 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.