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
1
vote
3 answers

Structure with same name, different definitions: segmentation fault with -O2

I've encountered a segmentation fault in a C++ program when two C++ files compiled together each include a different definition of a structure (with the same name). According to this question, I understand that structure definitions are restricted…
1
vote
2 answers

static variable inside function vs static class variable in c++

For a unique id for some object I can create a counter in two ways but I don't know which one is better while they are quite different in code (though maybe not in byte code, I have no idea). The first way would be to have some function which uses a…
ikku100
  • 809
  • 1
  • 7
  • 16
1
vote
2 answers

c++ variable/instance initializaiton order across different Translation units

Thanks in advance. I saw these codes in some real project. I just simplified them to express my problem here. The base class puts the this pointer into the a vector(vec) in its constructor. Using this trick, we can leverlage the dynamic bonding to…
1
vote
2 answers

Splitting code into files and O flags

When writing programs with code that can be executed in parallel in C, we definitely use the O flags to optimize the code. gcc -Olevel [options] [source files] [object files] [-o output file] In large projects, we usually split the code into…
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
0
votes
0 answers

ODR and C++ Versioning for Shared Library Wrapper

For simplicity, I will omit things like proper typedefs to opaque structs instead of void *, Windows calling conventions, fixed integer types, etc. Suppose I have the following files: CApi.h -- Shared library header with C linkage for portability…
0
votes
1 answer

How to have TU-specific function template instantiation?

Let's say: Some header h.hpp defines a template function f() using sizeof on its template parameter. Two different C++ source files, a.cpp and b.cpp, define their own structure having the same name S. Both a.cpp and b.cpp use f() with their own…
eepp
  • 7,255
  • 1
  • 38
  • 56
0
votes
1 answer

How a multiple times #included guarded header file will be inside different translation units?

I know that #inclusion is often described as a text copy-pasting preprocessor directive. Now if a header is #include guarded, or #pragma onced, then how'd we describe what is actually happening past the first translation unit to #include said…
0
votes
1 answer

Translation unit when including a source file?

As far as I know, a translation unit consists of a single implementation file .cpp/.c and all its included headers' code. When including a .cpp file inside another .cpp file, or including a .cpp file inside a .h file that is included inside yet…
Physician
  • 483
  • 2
  • 7
0
votes
1 answer

Does `#pragma GCC system_header` in a header file extend into another source or header file that includes it?

I need to disable all warnings inside a certain header file, and only that file only. The version of my compiler is g++-4.8. I have to use that compiler. I looked up in the documentation of that compiler: g++-4.8 documentation support for…
Galaxy
  • 2,363
  • 2
  • 25
  • 59
0
votes
2 answers

Best Way to Deal With Headers and Source Files

I always arranged my C++ headers and source files this way: prog.h #include class Prog { public: Prog(std::string); ~Prog(); void printName(); private: std::string…
DarkoNaito_09
  • 71
  • 2
  • 12
0
votes
2 answers

Is a header file a translation unit?

Is a header file a translation unit? If I add the static keyword to a variable in a header file, could I call that variable in my .c or .cpp file? Thanks.
Serket
  • 3,785
  • 3
  • 14
  • 45
0
votes
0 answers

Is assembly in C# similar to the translation unit in C++?

Is assembly in C# similar to the translation unit in C++? In C++, one .h, .cpp file is called a translation unit (preprocess finished state). Equally, is one .cs file called assembly in C#? Or is that the same as assembly in C++'s low level…
user3818260
0
votes
1 answer

Definitions of member fuction of class template full specialization in separate TUs

Given a class template, that take too long time to compile. During developement and debugging I want to reduce compilation time by separating the defintions of member functions into separate translation units. Just for the only full specialization…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
0
votes
2 answers

In singleton pattern what makes instance unique?

In practices singleton pattern is created with simple static function that returns one local static variable. As long as the instance is static it returns the same variable defined once during first function call. The confusing part for me is that…
0
votes
1 answer

Anonymous namespace in template implementation file

In a .cpp file, an anonymous namespace basically has file-wide linkage (after #includes) because the .cpp file will never be included by another file. But, the same pattern in a header file propagates that anonymous namespace to wherever it is…
Anthony
  • 1,015
  • 8
  • 22