0

When I compile this simple C++ file into an object file the function symbol does not get exported in the output object file.

// A.cpp
#include <iostream>
using namespace std;

extern inline void f1() {
  cout << "f1" << endl;
}

Compiled with: clang++ -c A.cpp -o A.o

When I check the exported symbols (nm A.o), I can see that the function has been excluded.

My understanding is that the 'extern' should specify that I want external linkage to be possible, so in this case why does it get excluded from the output object file? Removing the inline resolves the issue.

  • 1
    Related: [What does extern inline do?](https://stackoverflow.com/a/51229603/12002570) – Jason Jun 02 '22 at 07:52
  • @KenY-N, I don't think so. It has this statement: "extern inline: like GNU89 "inline": externally visible code is emitted, so at most one translation unit can use this. ". But, In my case, no externally visible code is emitted. – Rodrigo Salazar Jun 02 '22 at 07:55
  • Does it change with a separate function declaration? (I came to this after reading the accepted answer and comments to [What's the difference between static inline, extern inline and a normal inline function?](https://stackoverflow.com/a/25000931/7478597).) – Scheff's Cat Jun 02 '22 at 07:56
  • Providing a separate function declaration within the same cpp file does not change it. – Rodrigo Salazar Jun 02 '22 at 08:00
  • Please note, that the keyword `inline` is not responsible whether the compiler uses the opportunity to inline a function at a call side. This is something the compiler is permitted to do in every case. The meaning of `inline` has been changed in C++. Now, it is intended to provide a promise that every of multiple visible definitions will be exactly identical down to the tokens (so that the compiler may ignore repetitions without diagnostic or complaints - no violation of the ODR). Concerning this - `inline` in a `.cpp` file? Doesn't make much sense. – Scheff's Cat Jun 02 '22 at 08:02
  • Question was marked as duplicate even though I think the answers given in those questions conflict with what I am seeing (no symbol available in the generated object file). – Rodrigo Salazar Jun 02 '22 at 08:05
  • To elaborate my previous comment: [inline specifier](https://en.cppreference.com/w/cpp/language/inline) with focus on _There may be more than one definition of an inline function or variable (since C++17) in the program as long as each definition appears in a different translation unit_. As you wrote this in your `.cpp` file, the compiler may take this as "optimization" opportunity - and exclude it from the exported symbols. I'm not sure whether this is a valid assumption according to the standard although it "practically" seems so. – Scheff's Cat Jun 02 '22 at 08:13
  • 3
    It seems you are expecting `inline` to have the same meaning in C++ as in C. (Your quote about GNU89 is also about C.) That is not the case. Under C semantics of `inline` the function needs to be emitted, but it doesn't need to be emitted under C++ semantics. In C++, if a function is marked `inline`, it's definition _must_ be present in every translation unit using it, so the compiler doesn't need to emit it in any particular unit if it isn't used there. That's independent of the function's linkage or the `extern` specifier in C++ and extern linkage is the default anyway. – user17732522 Jun 02 '22 at 08:20

0 Answers0