0

What's the difference between these two non-member functions ?

static void
function1() {
    std::cout << "Test" << std::endl;
}

void
function2() {
    std::cout << "Test" << std::endl;
}

EDIT : I know static means that the function has internal linkage and by default a function in the global scope has external linkage but I don't really see how it will be different then. If we declare a function static or if we don't, we still have to include the file containing the function to use it in another file. It doesn't depend if the function is static or not, right ?

According to cppreference.com :

Internal linkage : The name can be referred to from all scopes in the current translation unit.

External linkage : The name can be referred to from the scopes in the other translation units.

Tesla123
  • 319
  • 1
  • 7
  • 4
    No, by default a function in the global namespace has external linkage. In anonymous namespaces, it has effectively internal linkage. – Quimby Jul 25 '23 at 12:21
  • Does this answer your question? [What is a "static" function in C?](https://stackoverflow.com/questions/558122/what-is-a-static-function-in-c) – Eldinur the Kolibri Jul 25 '23 at 12:22
  • @Quimby you're right Quimby, therefore I change my question. I think what I don't understand is the difference between internal linkage and external linkage. – Tesla123 Jul 25 '23 at 12:27
  • 4
    All of your questions are just one google search away. No need for formulating a question: https://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage – Eldinur the Kolibri Jul 25 '23 at 12:30
  • You've even given a perfectly valid definition. What do you not understand about it? Maybe refer to those answers: https://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c and https://stackoverflow.com/questions/11345631/what-does-scope-of-functions-mean – Eldinur the Kolibri Jul 25 '23 at 12:31
  • If I put static or if I don't, I don't see any difference in my program. I can use my function in both cases if I include the file containing it. So that's why I think I didn't understand the definitions of internal and external linkage well. – Tesla123 Jul 25 '23 at 12:34
  • 2
    Does [What is a "translation unit" in C++?](https://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c) help? – Ted Lyngmo Jul 25 '23 at 12:35
  • If you don't intend to use it in another translation unit then yeah.. It's best to make the function signature static (to express your intent). If you want to use the function in another TU, you have to expose it via a non-static function signature.. And if you only use one translation unit, it's a matter of style. But most people don't use static in that case. – Eldinur the Kolibri Jul 25 '23 at 12:37
  • Ok I see. A static non-member function is intended to be used only in the file where it's defined but it's possible to use it in another file all the same if we include the file where the static non-member function is. Quite strange tho. – Tesla123 Jul 25 '23 at 12:40
  • 1
    What do you mean by include? You should never include source files directly (only header files | in headers, you don't give a definition of functions, you simply declare them)!! You are misunderstanding some things severely about C++. Maybe just start by looking at a nice C++ book or follow tutorials in order. If you want to understand internal/external linkage and lack knowledge of how headers work, you should start by reiterating over basic concepts first before jumping to more advanced things. – Eldinur the Kolibri Jul 25 '23 at 12:45
  • I already have experience in C++. I never included a source file in another until now but I didn't think it was illegal. – Tesla123 Jul 25 '23 at 12:51
  • The answer below illustrates another nice usage. (If you want the functions to have the exact same name). And yes.. It's not illegal, but don't ever do it. Here is why: https://stackoverflow.com/questions/45110783/when-do-i-need-to-include-cpp-files – Eldinur the Kolibri Jul 25 '23 at 12:51
  • 3
    @Tesla123 You do not have to include a function to use it. You can declare `void foo();` in `a.cpp` and `b.cpp` independently. If you then define the function in e.g. `a.cpp` and link both TUs together, code in `b.cpp` can call the function just fine because the linker will link it to A TU's definition. If you define it in A as `inline`, then the linker will "not find" it in A TU because the symbol is not exported. That is the difference - external linkage= symbol is exported from TU where it was defined and available to other TUs, internal = not exported. Declarations of symbols are unrelated – Quimby Jul 25 '23 at 12:57

1 Answers1

-1

When you define a non-member function (or any data type for that matter) as static, you are essentially saying that this symbol is defined in this file and will not be referenced in other files. This is an important idea in linking, for example if I have two identical functions (assume that they are strongly defined as in my example, i.e. with a function body), and I don't label them as static, it will cause a linker error. But if I declare them as static, then the linker knows they are local to the file, and will treat it as OK.

file1

void doSomething(){} // LINKER ERROR

file2

void doSomething(){} // LINKER ERROR

OR

file1

static void doSomething(){} // OK

file2

static void doSomething(){} // OK

The static keyword can be used along with extern to manipulate the "visibility" of functions to the linker to deal with it. If you plan on using a non-member function in other files, you shouldn't label it as static.

Ethan
  • 11
  • 2
  • 1
    Even though you mention it in the text, in your examples you have declarations, no definitions. So there will at most be a linker error for a missing definition when testing the code. That may confuse a reader. Also, _visibility_ is a distinct concept in dynamic linking. – user17732522 Jul 25 '23 at 14:20
  • 1
    Um, with incorrect code examples and handwaving, this isn't really a helpful answer. – Pete Becker Jul 25 '23 at 15:31
  • Alright you got me, I was a little lazy this morning once I realized I needed to add some curly braces in my function. BUT, this goes to show exactly what is wrong with stackoverflow, instead of politely asking me to do what I said (have strongly defined functions), instead I am berated. Even better, you could've helped me understand what I did wrong so I can improve on my answers in the future. I rarely see any positivity left in this community and it is disheartening. – Ethan Jul 26 '23 at 02:08