how to compile c++ program when we r using two different functions which are defined in different file(s) under same directory in linux gcc???

Recommended Answers

All 10 Replies

gcc file1.cpp file2.cpp -o PROGRAM-Name

I guess something like that would do, Where Program-Name is the desired name of the program and file1 and file2 are your 2 different files(Dont forget to add the path).

However, It might put up some error's due to the order of declaration of the functions which can be circum-vented by adding a declaration to the function, Preferably with the keyword "extern"

>how to compile c++ program when we r using two different functions which are defined in different file(s) under same directory in linux gcc???

First look at Sky Diploma's post.

Second:
Well, I would type: [B]g++[/B] -o [I]outputfile[/I] [I]file1.cpp[/I] [ [I]file2.cpp[/I] ] [ file[I]x[/I].cpp ] The square brackets indicate an optional element and outputfile is the name you want to give to your executable file.
After compiling you can run your program by entering: ./outputfile on your keyboard.

@Sky Diploma:
The gcc command invokes the C compiler, not the C++ compiler.

Member Avatar for onaclov2000

Wouldn't you just use a "include" statement and include the second file in the first, then it's just a normal compile and the compiler will handle what files need to be compiled due to direct dependencies, or am I just not understanding the question????

Wouldn't you just use a "include" statement and include the second file in the first, then it's just a normal compile and the compiler will handle what files need to be compiled due to direct dependencies, or am I just not understanding the question????

I've to admit that I've never seen this before.
But I have a strange feeling that this is an inappropriate way, not sure though why, I think it can cause naming conflicts.

Okay,

using #include "anotherfile.cpp" does cause naming conflicts, because it will cause multiple files to contain copies of definitions for the same function, use header files to declare functions. Also build with separate compilation and linking stages.

If you want to compile from the command line look into makefiles or something similar.

Member Avatar for onaclov2000

The original post doesn't specify if it's a header file or not, thus my confusion, typically if you write a function in your header file, then #include it would that not be a legitamite way of doing it? This is of course we are talking about the function being in a header file.

Understand what exactly #include<header.h> does:
It simply copy-paste the content of header.txt and replaces it with the line containing #include<header.h>
Say, if you have a header.h with the following :

+--header.h---------------------------------+
+-------------------------------------------+
|   Sticks and Stones can break my bones.   |
|   But they can't break my soul.           |
|___________________________________________|

and now let the following file #include the above file:

+--myfile.cpp-------------------------------+
+-------------------------------------------+
|   The content of the file is:             |
|   #include "header.h"                     |
|___________________________________________|

The preprocessor will automatically output the following into the compiler:

+--myfile.cpp---after being fed into preprocessor---+
+---------------------------------------------------+
|   The content of the file is:                     |
|   Sticks and Stones can break my bones.           |
|   But they can't break my soul.                   |
|___________________________________________________|

If you are using g++ and have facility of grep, you can use the following commands to see the output of the preprocessor

$: g++ myfile.cpp -E | grep -v "#"

note that ``$:" is the prompt character of my shell.
Remember that the above examples are not valid C++ program and hence they cannot be fed into a compiler without producing errors.

When you write a header file or a library (say, a class), you usually divide it into two files:
1. First file containing the declarations (or as many people says, containing the `interface') of your class. This file is conventionally is suffixed by the `.h' header extention. This file is #included in your main cpp file, say `main.cpp'

2. And the second file, containing the definitions (or as many people says, containing the `implementation') of your class. This file is named as a `.cpp' file. This file #includes the file1 so as to make it compilable.

Strictly speaking, your compiler do not need the implementation file of your class. It is happy to accept just the declaration and compile your code. The job of the compiler is just to compile all the `.cpp' file which was supplied to it in the argument, produce their compiled versions and fed them into a Linker.
Linker, is the software which actually needs the implementation too. Here is what it does:
It takes the compiled version of your `main.cpp' (note that `main.cpp' has #included the `header.h' declarations) and try to look for the definition of your class members. If the definitions of your class members is found in the same compiled code, it accepts it and build your executable. If it does not, it looks for the other files which were fed into it by the compiler to check if they contain the definition of the class members. If it finds it, good. It produces your executable. If it doesn't it raises an error saying that he could not find any reference to the definitions of the members.

So, how do you manage multi-file programs?
First, you *compile* all of your cpp files. I repeat: just compile it, do not link it yet. In g++, this is done by the -c switch. So here is what you need to do for our example:

$: g++ main.cpp -c -o main.o
$: g++ header.cpp -c -o header.o

Note that the -o switch is for :"rename the output file as"

After this step, all of your code is compiled:
- the `main.cpp' is compild into `main.o' along with the `header.h' and
- the `header.cpp' is also compiled into `header.o'. Note that `header.h' also gets compiled with it as `header.cpp' #included the `header.h'

I repeat, all the source is now compiled but not linked yet. To link it, you again call the g++ with all the compiled files:

$: g++ main.o header.o -o output.exe

Now, the g++'s linker will link your compiled code into an executable: output.exe

This two step process can be shortened by compiling and linking the two files in one go:

$: g++ main.cpp header.cpp -o output.exe

This command says: "compile main.cpp and header.cpp and feed the output to the linker to get output.exe"

Note that in larger projects, one should use utilities such as "make" to ease out the compilation process.


But I don't wan't to separate implementation with interface. Can I put both in one file??
Yes you can. You can structure your code something like this:

+--header.h------------------+
 +----------------------------+
 | //declarations             |
 | class MyClass              |
 | {                          |
 |     private:               |
 |         int foo;           |
 |     public:                |
 |         int bar(int);      |
 | }                          |
 | //here goes the definition |
 | int MyClass::bar(int i)    |
 | {                          |
 |     return ++i             |
 | }                          |
 +----------------------------+

and now simply include the .h file:

+--main.cpp-----------+
 +---------------------+
 | #include <iostream> |
 | #include "header.h" |
 | main()              |
 | {                   |
 |   //blah blah       |
 | }                   |
 +---------------------+

and simply compile it as :

$: g++ main.cpp -o output.exe

and you are done.
However this is not recommended for larger projects.

As a consequence of this, you could only include your implementation file of your class in the `main.cpp' and get over with. For example, lets say your code is distributed as:

+--header.h------------------+
 +----------------------------+
 | //declarations  only       |
 | class MyClass              |
 | {                          |
 |     private:               |
 |         int foo;           |
 |     public:                |
 |         int bar(int);      |
 | }                          |
 +----------------------------+
+--header.cpp----------------+
 +----------------------------+
 | #include "header.h"        |
 | //definitions  only        |
 | int MyClass::bar(int i)    |
 | {                          |
 |     return ++i             |
 | }                          |
 +----------------------------+

you could #include the `header.cpp' and write:

+--main.cpp-------------+
 +-----------------------+
 | #include <iostream>   |
 | #include "header.cpp" |
 | main()                |
 | {                     |
 |   //blah blah         |
 | }                     |
 +-----------------------+

and compile it as

$: g++ main.cpp -o output.exe

This will work, I don't recommend it however. This is the poorest choice.

However the following will not work, it will cause naming conflicts:

+--main.cpp-------------+
 +-----------------------+
 | #include <iostream>   |
 | #include "header.h"   |
 | #include "header.cpp" |
 | main()                |
 | {                     |
 |   //blah blah         |
 | }                     |
 +-----------------------+

here, you #included both the cpp and .h files.
Why this causes name conflicts is clear: because the header.h gets included twice; one from header.cpp and another from main.cpp itself.
This can be verified by using the

$: g++ main.cpp -E | grep -v "#"
commented: Excellent post, truly excellent. +36
commented: I agree... You've got too much free time :) +21
commented: Maybe not posting much (in you signature), but when your post, the result is always of high quality :) +13
Member Avatar for onaclov2000

However the following will not work, it will cause naming conflicts:

+--main.cpp-------------+
 +-----------------------+
 | #include <iostream>   |
 | #include "header.h"   |
 | #include "header.cpp" |
 | main()                |
 | {                     |
 |   //blah blah         |
 | }                     |
 +-----------------------+

here, you #included both the cpp and .h files.
Why this causes name conflicts is clear: because the header.h gets included twice; one from header.cpp and another from main.cpp itself.
This can be verified by using the

$: g++ main.cpp -E | grep -v "#"

Wouldn't a simple If Not Defined wrapped around the include solve the "conflict"?

Sorry I'm still learning too, so far this has been a quite informative post and I didn't even ask the question....LOL

Wouldn't a simple If Not Defined wrapped around the include solve the "conflict"?

Yes. Even better is the usual convention of wrapping everything in the header with a conditional compile test:

#ifndef HEADER_H
#define HEADER_H

// header contents

#endif

Then it doesn't matter how many times the header is included, or if it is included recursively. The translation unit will only have one copy of the contents and you don't have to worry about conditionally compiling the include directives one by one to fix ambiguities.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.