top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Which is the standard way of compiling multiple file?

+1 vote

Please assume that I don't want to create library file

Lets say,
I have 2 files main.cpp and function.cpp. I have 2 methods to compile both the file?

1st) while compiling just include both files,

   g++ main.cpp function.cpp

2nd) In main.cpp file i can include function.cpp

#include "function.cpp"

As far as I know while compiling using 2nd method it will take more time, other than that any advantage/disadvantage?
Also what if i have more files lets say i have 500+ files, all are linked with each other, then which method will be preferable?
In this case if I go with 1st method then I will have to provide 500+ file names in my makefile?

Can anybody help?

posted Aug 26, 2015 by Chirag Gangdev

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

I hope I got your problem correctly
In big solutions say you have the 100+ .c/.cpp files the correct steps to compile them should be
1. Create .o file for each C/CPP file (include the header file in the C file as per logic, inclusion is a preprocessor command which is essentially a replace before compilation so you should never include .C/.CPP file in another), this should be done for the .c/.cpp file which has main function also.
2. Once all .o's are created then create the executable using ar or gcc itself.

Most likely the above steps should be done using a Makefile so that future addition or deletion or different compilation option is easy.

answer Aug 26, 2015 by Salil Agrawal
Suppose i have 100+ files than i will have to include each and every files in Makefile right?
Yes its the way, its to be done once. Including .c in another is not the correct way suppose you have same static function defined in multi files then u can see errors.
Ok. Thanks Sir...
Similar Questions
+4 votes

Lets assume I have a system with RAM of 1GB. and virtual memory is 500MB. That brings to 1.5GB i.e. 1500 MBytes.

I have read somewhere that when I process is created stack of 8MB is associated to that process. So, assuming that any of the process is not allocating any dynamic memory or anything, then does it mean that, Maximum number of process that i can create is 1500/8 and i.e. 187 Process.

Please clarify my understanding,

+1 vote

Let me put some context of my curiosity -
One of the mysterious activities in Programming Language design is the aspect of standard libraries and there have been talk of "modernizing" standard libraries to meet up to the requirements:
i) Parallelism(multi-core)
ii) Searching
iii) Big data
iv) GUI lacking in C & C++
v) The top N widely used algorithms

Now as a developer I am interested to know, Is there a precise formula for the evolution of Standard Libraries and measure its effectiveness?

+1 vote

I wrote a multi-threaded program using pthread library, but I want execution of threads in a specific order.
What I have to do ?

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.