user3272255
user3272255

Reputation: 11

How do I compile a program in G++ on Ubuntu, having multiple source files?

I have programme main.cpp that call other C++ programme file1.cpp, file2.cpp and message.txt

Please how can I write a command for compilation in Linux Ubuntu using g++, the main is not "void" it is written this way:

#include <iostream>
#include <fstream.h>
#include "file1.h"
#include "file2.h"

int main( int argc, const char* argv[] ) 
{
    if( (argc != 2) && (argc != 4) ) 
    { ...

Upvotes: 1

Views: 132

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54325

To build it:

g++ -Wall -Wextra -g file1.cpp file2.cpp main.cpp -o program

Other good flags to add to g++ are -std=c++11 for C++ 11 support and -O2 -DNDEBUG for optimized release builds.

To run it:

./program

Also, you should try to learn about make files. A makefile is a script run by make and it saves a recipe for building programs. Once your program gets more than a couple of source files, a makefile is a good idea.

Then, past makefiles are tools like autotools or CMake, which make makefiles for you.

Upvotes: 6

Related Questions