Questions tagged [translation-unit]

A translation unit is the basic unit of compilation according to standard C++. It consists of the contents of a single source file, plus the contents of any header files directly or indirectly included by it, minus those lines that were ignored using conditional preprocessing statements.

A translation unit is the basic unit of compilation according to standard C++. It consists of the contents of a single source file, plus the contents of any header files directly or indirectly included by it, minus those lines that were ignored using conditional preprocessing statements.

A single translation unit can be compiled into an object file, library, or executable program.

The notion of a translation unit is most often mentioned in the contexts of the One Definition Rule, and templates.

50 questions
30
votes
4 answers

Why does defining inline global function in 2 different cpp files cause a magic result?

Suppose I have two .cpp files file1.cpp and file2.cpp: // file1.cpp #include inline void foo() { std::cout << "f1\n"; } void f1() { foo(); } and // file2.cpp #include inline void foo() { std::cout <<…
Narek Atayan
  • 1,479
  • 13
  • 27
16
votes
1 answer

Translation unit vs Compilation unit vs object file vs executable vs.... in C++

I couldn't find the difference between translation unit, compilation unit, object file, executable...At many places I've seen that one is used instead of other. I am aware that these files are generated during C++ program compilation and linkage.…
pasha
  • 2,035
  • 20
  • 34
13
votes
2 answers

c++ How to declare a class as local to a file

So, I know static functions are functions that are local to the file. Thus, they can't be accessed from other files. Does this work for classes too? I've read a ton of controversy on how static class does not declare the class to contain purely…
Codesmith
  • 5,779
  • 5
  • 38
  • 50
12
votes
1 answer

Why shouldn't C++ operator new/delete/variants be in header files?

Can someone explain the nature of this C++ compile error? I am dabbling in/learning about overloading the global operators new, delete, and their variants. I read a couple of articles on the subject, but I couldn't find one that seems to address…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
11
votes
3 answers

Will instantiating templates in precompiled headers reduce compile times?

Example: Say I include in my precompiled header file: #include As a few instances of the vector, such as std::vector, std::vector etc are used often in my project, will it reduce compile time if I instantiate them as well in the…
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
7
votes
1 answer

c++17 inline + thread_local vs thread_local

I am wondering what exactly the difference between the following two declarations is if both are written in a single header file: inline thread_local MyClass obj1; // inline with thread_local thread_local MyClass obj2; // no inline As…
Jes
  • 2,614
  • 4
  • 25
  • 45
7
votes
3 answers

inline function in different translation units with different compiler flags undefined behaviour?

in visual studio you can set different compiler options for individual cpp files. for example: under "code generation" we can enable basic runtime checks in debug mode. or we can change the floating point model (precise/strict/fast). these are just…
7
votes
2 answers

C: clarification on translation unit

If we have two .c files and a .h file: main.c sub.c sub.h, where main.c #include "sub.h" ... sub.c #include "sub.h" ... we can compile the program with, either i) gcc -o a.out main.c sub.c or ii) gcc -c main.c gcc -c sub.c gcc -o a.out main.o…
ElectroJunkie
  • 301
  • 5
  • 16
5
votes
3 answers

How can static local variable shared along different translation unit?

How can static local variable shared along different translation unit? I know that "static" specifies internal linkage. In case of "static" member function having same address in different translation units, each translation unit has a copy of the…
YoonSeok OH
  • 647
  • 2
  • 7
  • 15
4
votes
1 answer

How to avoid deferring the initialization of static storage duration variables?

I have classes with side effects in their constructors, and objects of these classes are global objects that have static storage duration. During the initialization these objects register their classes in a special map, and it is important for these…
3
votes
2 answers

constexpr function which is shared in multiple modules

I noticed a strange behavior when I was working with a constexpr function. I reduced the code to a simplified example. Two functions are called from two different translation units (module A and B). #include int mod_a(); int…
MiCo
  • 399
  • 2
  • 6
3
votes
1 answer

Why the weak symbol defined in the same .a file but different .o file is not used as fall back?

I have below tree: . ├── func1.c ├── func2.c ├── main.c ├── Makefile ├── override.c └── weak.h main.c invokes func1(). func1() invokes func2(). weak.h declares func2() as weak. override.c provides an override version of func2(). func1.c #include…
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
3
votes
2 answers

Can a define be made across all translation units?

Can a #define or similar pre-processor definition be made across all translation units? Header implementations are useful for really small libraries since all the code can be contained and distributed with a single header with the following…
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
3
votes
1 answer

Are multiple source files being passed to gcc treated as a single translation unit?

I think I've read that compiling multiple files with gcc at the same time would achieve the same thing as adding all sources into a single source file, as per Single Compilation Unit, but I can't find any sources on that anymore. Is that true? We…
Spidey
  • 2,508
  • 2
  • 28
  • 38
3
votes
1 answer

Is static deprecated when ensuring availability between translation units?

From the following stackoverflow answer, the user says: It means that the variable is local to a translation unit (simply put, to a single source file), and cannot be accessed from outside it. This use of static is in fact deprecated in the…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
1
2 3 4