#Separate Compilation

58 messages · Page 1 of 1 (latest)

spice tangle
#

why do sometimes separate compilation using
a.cpp a.h b.cpp b.h c.cpp c.h main.cpp ( and for example put declaration in a.h and put definition in a.cpp)
but not use
a.h b.h c.h main.cpp ( put both declaration and definition in a.h)
would the second way be more convenient because we can use headers as plug in in different cpp files or projects,
and we just copy the header only and repeat using it in different cpp files or project, do not need to copy both ( example: a.h and a.cpp )
why still use first way?
Let me clarify, I want to know which is better or which is better in which conditions or uses

cyan ironBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dusty perch
#

Having only headers equals slower compilation times

spice tangle
#

why

dusty perch
#

#include pastes the contents of the headers in place of itself during compilation

#

So everything in the headers is recompiled every time a .cpp file including them is compiled

spice tangle
#

so it is use to reduce

#

compilation time

#

because include is include whole bunch of things

#

what about the redefinition case

#

is it concerned?

dusty perch
#

What redefinition case?

dusty perch
# spice tangle compilation time

Yeah, and also it lets you rebuilt only the parts of your code that have changed. If you have multiple .cpp files and your changes don't affect all of them, you can only rebuild those that you changed (and if you changed headers, the .cpp files that include them)

spice tangle
#

redefinition and the headers file problem

#

ifndef

#

sth sth

dusty perch
#

There are two separate problems. One is including a header more than once per TU (= translation unit = .cpp file with all included headers). This happening is normal, and you avoid the redefinition errors using either #pragma once or an include guard (yoru ifdef)

#

Another problem is having function definitions (as opposed to declarations) in headers causes redefinition errors at link time (as opposed to compile-time), because multiple .cpp files end up defining the same function. This is solved by marking the functions inline

spice tangle
#

ok, I watch youtube tutorial myself and a guy say it is related

#

to separate compilation

#

correct or incorrect?

dusty perch
#

Uhh, of course it's related, but just a yes/no answer probably isn't very useful

spice tangle
#

Can i know how compilers works to compile >2 cpp files in binary

#

first, one definition rule works among multiple c++ files right?

dusty perch
spice tangle
#

ok

#

also, i see weired things in my assignment

#

the starting code

#

is that #include "xxx.cpp" also works?

#

but it is not taught

dusty perch
#

That .cpp file should be compiled separately. If you both compile it separately and include it, you get redefinition errors during linking

spice tangle
#

because the assignment is made by multiple assistant professors , so i dont know who make this things

dusty perch
#

Also by including it, you get none of the benefits of separate .cpp files (for the compilation speed)

spice tangle
#

yah. i ask chatgpt , chatgpt say it is bad too

#

just forget all the things.
lets summarize
first
separate a.h to a.cpp and a.h -> increase compilation speed
since you are using c++ not python, you desire a faster compilation speed
( you can still do in the original way but sacrifice compilation speed )
second
headers guard ( ifndef progma once inline ) -> prevent redefinition
(one definition rule works among multiple c++ files)
third
include c++ files directly messed up

cyan ironBOT
#
Large Language Models (LLMs)

We highly recommend against the use of LLMs and AI assistants because:

  • LLMs are bad at C and C++
  • LLMs are wrong more often than not
  • LLMs answer with complete confidence even when wrong
  • If you're new to C or C++ you likely don't know enough to know when answers are wrong
dusty perch
#

Include guards don't help with redefinition across multiple files

#

Honestly, just try it, see what happens when you omit them

#

And then see what happens when you define functions in headers without inline

spice tangle
#

ok, lets put a solve if there is any problem, i start a new problem here, thankyou

#

!solved

cyan ironBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

spice tangle
#

I will try

lavish egret
#

@spice tangle ^^

spice tangle
#

thankyouuuu

spice tangle
# lavish egret

I think the problem is if you put definition in foo.h, You wont want to make a new foo.cpp and cause an error

#

compare

foo.h

void f();

foo.cpp

#include <iostream>
void f() { std::cout << "Hello World!\n"; }

bar.cpp

#include "foo.h"
int main() 
{
f();
return 0;
}
g++ foo.cpp bar.cpp -o app

and
foo.h

#include <iostream>
void f() { std::cout << "Hello World!\n"; }

bar.cpp

#include "foo.h"
int main() 
{
f();
return 0;
}
g++ bar.cpp -o app

and
foo.cpp

#include <iostream>
void f() { std::cout << "Hello World!\n"; }

bar.cpp

#include "foo.cpp"
int main() 
{
f();
return 0;
}
g++ bar.cpp -o app
dusty perch
lavish egret
#

Please read what I've sent instead of disagreeing without consideration...

#

Because you are simply wrong

#

You will get linker errors if you put the definition is foo.h (without the inline specifier) and then you include the header in multiple translation units