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
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
  • 653
  • 3
  • 9
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
  • 1,795
  • 2
  • 16
  • 37
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
  • 199
  • 8
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
  • 1,885
  • 16
  • 32
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
  • 44,504
  • 107
  • 265
  • 458
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
  • 5,419
  • 7
  • 34
  • 73
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
  • 688
  • 5
  • 14
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,652
  • 2
  • 23
  • 37
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
  • 1,856
  • 1
  • 14
  • 39
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…
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
  • 1,865
  • 1
  • 5
  • 23
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