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
3
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
  • 12,806
  • 9
  • 48
  • 94
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
  • 15,941
  • 8
  • 72
  • 167
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…
1
vote
1 answer

Is a translation unit valid C++? And is a translation unit (for GCC) the output of g++ -E?

I was convinced that a translation unit is a .cpp file (or, to avoid referring to an extension, a file you would feed to `g++ -c theTranslationUnit.cpp -o whatever.o) once you substituted into it the macros, copied and pasted the #includes…
Enlico
  • 23,259
  • 6
  • 48
  • 102
1
vote
2 answers

Is there any reason I should include a header in the associated cpp file if the former only provides declarations the latter defines?

Consider a foo.cpp file with the following content #include "foo.hpp" int foo() { return 7; } and its associated header #pragma once int foo(); The latter is obviously needed to make aware the following main function of the existence of…
Enlico
  • 23,259
  • 6
  • 48
  • 102
1
vote
1 answer

How many translation units in one module?

Does a module with multiple source files (.cpp) have one or multiple translation units? My understanding is that every single source file (.cpp) will be its own translation unit unless it is included, and #pragma onced (which I guess is a…
Physician
  • 483
  • 2
  • 7
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
  • 927
  • 1
  • 5
  • 16
1
vote
1 answer

Splitting Boost.Spirit.X3 parsers into several TUs

I'm struggling with Boost.Spirit.X3 again. I have several logical groups of parsers (statements, expressions, etc.), each of which is represented by several files: group.hpp - contains typedefs, BOOST_SPIRIT_DECLARE and extern variable declaration…
GooRoo
  • 661
  • 3
  • 9
1
vote
1 answer

Class methods internal linkage

In C I can have a structure and some public functions declared in a header file, while some "private" functions can be declared as static in a source file. For example: foo.h typedef struct Foo { ... } Foo; void func1(Foo *foo); foo.c #include…
Leonid
  • 1,127
  • 2
  • 15
  • 29
1
vote
2 answers

Should I declare my function template specializations or is defining them enough?

I have some classes which can be checked. The code which implements this declares a function template in a header file and specializes it in different source files: // check.h template bool check(const T& object); // class1.h struct…
1
vote
1 answer

Could I provide same function definition in different TUs

I was reading about internal and external linkage, and I found that by default a function has an external linkage. So I was thinking if is it possible to declare a function in a header filer and provide multiple definitions of it in different…
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
1
vote
2 answers

Is there a way to have a linker pull part of an object file from a library for linking?

I have a project with thousands of C files, many libraries, and dozens of programs to link, and to speed up the compilation, I am combining C files into translation units that include multiple C files. This is sometimes referred to as single…
Bill Morgan
  • 538
  • 2
  • 14
1
vote
2 answers

What do the lines starting with # symbol in g++ -E generated translation unit

I tried to check the translation unit generated for a simple hello world program looks like. So, I wrote below code in test.cpp. #include using namespace std; int main() { cout<<"Hello World"<
pasha
  • 2,035
  • 20
  • 34
1
vote
3 answers

Internal Linkage variables in header files - Does a variable is allocated in memory each time the header is included?

Let say I have an header file Resources.h where I have defined these 5 structs: const IColor COLOR_BLACK(255, 0, 0, 0); const IColor COLOR_GRAY(255, 127, 127, 127); const IColor COLOR_WHITE(255, 255, 255, 255); const IColor COLOR_RED(255, 255, 0,…
markzzz
  • 47,390
  • 120
  • 299
  • 507
1
vote
0 answers

When can symbol accessed from template be declared later

This example compiles: #include template int foo(const T* bar,size_t N) { return test(T{}); } struct Type { }; inline constexpr int test(Type) {return 0;} int main() { Type vals[4]; return foo(vals,4); } For other…
user877329
  • 6,717
  • 8
  • 46
  • 88