What is a translation unit ?

What is a translation unit? What does it mean when a function is not used in the translation unit?
What the compiler gets after the preprocessor finishes with it.
https://en.wikipedia.org/wiki/Translation_unit_(programming)

For your purposes, you can consider it to be a .cpp file (and its associated .hpp file).
So it means that the function is called from outside the name.cpp and name.hpp files? So make it so it is called in the translation unit do I just use the scope operator?
Like Duoas says, a translation unit is a bunch of text that the compiler gets once all the lines beginning with the hash # are processed by the preprocessor.

Each "source file" and any files that it #includes (and any files that those included files #include) form a translation unit.

If a function isn't used in a translation unit then you haven't called it in that bunch of code. The scope operator is only used if the function is in a local scope.

Last edited on
Oh, so all I need to do is include the files it is used in the the header? with the #includes?
http://www.cplusplus.com/faq/beginners/multiple-sources/

The compiler only knows how to handle ONE SINGLE FILE at a time. It does not understand or know anything about functions declared elsewhere except for what you tell it.

For example, the standard C program is:

1
2
3
4
5
6
#include <stdio.h>
int main()
{
  puts( "Hello world!" );
  return 0;
}

The #include stuff just copies the stuff in "stdio.h" (from wherever the compiler keeps the "stdio.h" file) and pastes it verbatim into your .c file. The .h file tells the compiler what stuff exists elsewhere.

You could do the same yourself:

1
2
3
4
5
6
7
int puts( const char* );  // prototype for puts(), which is declared elsewhere

int main()
{
  puts( "Hello world!" );
  return 0;
}

The .h files just save us a lot of typing: instead of a gazillion lines of code telling us about stuff found elsewhere for every single .c or .cpp file we wish to compile, we can just #include that information in a single line.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.