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.

41 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,459
  • 13
  • 27
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
  • 3,821
  • 11
  • 42
11
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
  • 4,483
  • 4
  • 32
  • 43
11
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
  • 1,885
  • 16
  • 32
9
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,172
  • 3
  • 52
  • 78
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
  • 15
5
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,132
  • 1
  • 18
  • 33
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
  • 10,856
  • 7
  • 40
  • 83
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,248
  • 1
  • 23
  • 32
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
  • 31,728
  • 22
  • 131
  • 236
3
votes
1 answer

Using functions that return placeholder types defined in another translation unit

I'm having some trouble understanding how the C++14 extension of the auto type-specifier described in N3638 can possibly be implemented, and what, exactly, is allowed. Specifically, one of the changes to the standard says, If the declared return…
Kyle Strand
  • 14,120
  • 3
  • 59
  • 143
2
votes
2 answers

Prevent same macro having different definitions in multiple translation units

I'm creating a library that will need different command-line defined macros (-D option) per resulting binary (.exe, .so, .dll) that uses it. I would like to ensure each translation unit that will be a part of resulting binary, was compiled with the…
2
votes
1 answer

Template instance in different translation units

As far as I know, each template have different instances on each translation unit, and for my understanding a translation unit is roughly a cpp file. So, if I have a file named test.hpp with the following contents: // test.hpp template
PaperBirdMaster
  • 11,602
  • 9
  • 41
  • 84
1
vote
2 answers

Including multiple .c files in a single translation unit

In C is it recurrent to have .c files including other internal .c files with static variables / functions in a copy / paste manner? Like a .c file composed of many .c files where you want everything to be kept private and not declared in a header…
João Pires
  • 803
  • 5
  • 14
1
2 3