-2

now in real world when building projects we use hunderends of .cpp files and we link them into one main.cpp file.
Main.cpp

#include <iostream>
#include <function>
int main()
{
    print("hello");
}

function.cpp
#include <iostream>
using namespace std;
#include <string>
void print(string s)
{
    cout << s;
}

I'm using c++(microsoft) on visual studio code
terminal

C:\Users\Sainath\Desktop\Project>cl main.cpp
C:\Users\Sainath\Desktop\Project>main

then we get an output
output

hello

Is there any way to do something like this? I could use Visual studio to build and run it as one whole .cpp, but I want to understand on how this thing works

edit: I mean we dont explicitly compile iostream but it stil gets compiled during preprocessor time, Is there any way to do automate function.cpp compilation without explicitly doing it?

` Blockquote

`

  • *"Is there any way to do something like this?"*. Refer to [Separating class code into a header and cpp file](https://stackoverflow.com/questions/9579930/separating-class-code-into-a-header-and-cpp-file). Provide the declaration for the function in header and implementation in source file and include the "header" where you want to use that function. – Anoop Rana May 14 '22 at 10:39
  • no not into header, https://youtu.be/SfGuIVzE_Os in this video at 14:43 he kinda builds and runs the code in one go, he doesn't create an header file – Rambal heart remo May 14 '22 at 10:42
  • 2
    Yes there are many ways. You can use `CMake` or `Make` etc to automate the process. This is the recommended way anyway when you're working with large projects. There is a nice example given here at [CMake tutorial](https://riptutorial.com/cmake). – Anoop Rana May 14 '22 at 10:44
  • *"gets compiled during preprocessor time"* That's wrong. The preprocessor combines the includes with the including file to a single translation unit that gets compiled in one go, at least ignoring optimizations that don't change the observable behaviour. No seperate compilation takes place. Note that there likely is a library containing implementation of logic used in the standard library headers though, but that's already provided in compiled form (libc/libc++) – fabian May 14 '22 at 10:48

0 Answers0