2012-02-22 21 views
6

私はいくつかのマクロサブルーチンをインライン関数に置き換えようとしているので、コンパイラはそれらを最適化できるので、デバッガはそれらにステップインすることができます。Cのインライン関数と "未定義の外部"エラー

void do_something(void) 
{ 
    blah; 
} 

void main(void) 
{ 
    do_something(); 
} 

が、私は、インラインとしてそれらを定義した場合::作品

inline void do_something(void) 
{ 
    blah; 
} 

void main(void) 
{ 
    do_something(); 
} 

それは ":未定義の外部エラー" と言います。どういう意味ですか?暗闇の中で刺すと、私は試しました

static inline void do_something(void) 
{ 
    blah; 
} 

void main(void) 
{ 
    do_something(); 
} 

これ以上のエラーはありません。関数の定義と関数の呼び出しは、同じ.cファイル内にあります。

誰かがなぜ動作するのか、他のものが動作しないのか説明できますか?

(第2の関連質問:?私は複数の.cファイルでそれらを使用したい場合、私はインライン関数を入れてください)

+0

、あなたはこれらのエラーを取得しているとき。いくつかはヘッダーにありますか?ヘッダーで宣言して別のコンパイル単位から使用しようとしていますか? – Mat

+0

ああ、関数定義と関数呼び出しは、どちらも同じ.cファイルにあります。 – endolith

+0

あなたは[sscce](http://pscode.org/sscce.html)を投稿できますか? (もちろん、この場合はコンパイルできません) – Mat

答えて

9

まず、コンパイラは常にinlineとマークされた関数をインライン化するとは限りません。たとえば、すべての最適化をオフにすると、おそらくインライン展開されません。

あなたはインライン関数を定義

inline void do_something(void) 
{ 
    blah 
} 

とその機能を使用し、それは暗黙的に「のextern」であるためにも、同じファイルに、その関数の呼び出しは、リンカではないコンパイラによって解決されます。しかし、この定義だけでは、関数の外部定義は提供されません。

あなたがinline定義を参照することができますCファイル内inline

void do_something(void); 

せずに宣言が含まれている場合、コンパイラは、関数の外部定義を提供し、エラーが離れて行く必要があります。

static inlineの理由は、そのコンパイルユニット内でしか関数を表示できないため、コンパイラは関数の呼び出しを解決して最適化し、そのコンパイル単位内の関数のコードを出力することができます。リンカーはそれを解決する必要はないので、外部定義は必要ありません。

インライン関数を配置する最も良い場所は、ヘッダファイルにあり、static inlineと宣言します。これにより、外部定義の必要性がなくなり、リンカーの問題が解決されます。しかし、これにより、コンパイラはそれを使用するすべてのコンパイル単位で関数のコードを発行します。その結果、コードが肥大化する可能性があります。しかし、関数はインラインであるため、とにかくおそらく小さいので、これは通常問題ではありません。

他のオプションは、ヘッダ内extern inlineとしてにそれを定義し、1つのCファイル内にあるinline修飾語なしで提供し、extern宣言

gccのマニュアルは、このようにそれを説明する:あなたはそれらの定義を入れているところそれは不明だ

By declaring a function inline, you can direct GCC to make calls to that function faster. One way GCC can achieve this is to integrate that function's code into the code for its callers. This makes execution faster by eliminating the function-call overhead; in addition, if any of the actual argument values are constant, their known values may permit simplifications at compile time so that not all of the inline function's code needs to be included. The effect on code size is less predictable; object code may be larger or smaller with function inlining, depending on the particular case. You can also direct GCC to try to integrate all "simple enough" functions into their callers with the option -finline-functions .

GCC implements three different semantics of declaring a function inline. One is available with -std=gnu89 or -fgnu89-inline or when gnu_inline attribute is present on all inline declarations, another when -std=c99 , -std=c1x , -std=gnu99 or -std=gnu1x (without -fgnu89-inline), and the third is used when compiling C++.

To declare a function inline, use the inline keyword in its declaration, like this:

static inline int 
inc (int *a) 
{ 
    return (*a)++; 
} 

If you are writing a header file to be included in ISO C90 programs, write __inline__ instead of inline .

The three types of inlining behave similarly in two important cases: when the inline keyword is used on a static function, like the example above, and when a function is first declared without using the inline keyword and then is defined with inline , like this:

extern int inc (int *a); 
inline int 
inc (int *a) 
{ 
    return (*a)++; 
} 

In both of these common cases, the program behaves the same as if you had not used the inline keyword, except for its speed.

When a function is both inline and static , if all calls to the function are integrated into the caller, and the function's address is never used, then the function's own assembler code is never referenced. In this case, GCC does not actually output assembler code for the function, unless you specify the option -fkeep-inline-functions . Some calls cannot be integrated for various reasons (in particular, calls that precede the function's definition cannot be integrated, and neither can recursive calls within the definition). If there is a nonintegrated call, then the function is compiled to assembler code as usual. The function must also be compiled as usual if the program refers to its address, because that can't be inlined.

Note that certain usages in a function definition can make it unsuitable for inline substitution. Among these usages are: use of varargs, use of alloca, use of variable sized data types , use of computed goto, use of nonlocal goto, and nested functions. Using -Winline will warn when a function marked inline could not be substituted, and will give the reason for the failure.

As required by ISO C++, GCC considers member functions defined within the body of a class to be marked inline even if they are not explicitly declared with the inline keyword. You can override this with -fno-default-inline .

GCC does not inline any functions when not optimizing unless you specify the always_inline attribute for the function, like this:

/* Prototype. */ 
inline void foo (const char) __attribute__((always_inline)); 

The remainder of this section is specific to GNU C90 inlining.

When an inline function is not static , then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non- static inline function is always compiled on its own in the usual fashion.

If you specify both inline and extern in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it.

This combination of inline and extern has almost the effect of a macro. The way to use it is to put a function definition in a header file with these keywords, and put another copy of the definition (lacking inline and extern) in a library file. The definition in the header file will cause most calls to the function to be inlined. If any uses of the function remain, they will refer to the single copy in the library.

+0

"まず、コンパイラはインラインでマークされた関数を必ずインライン化するとは限りません。コンパイラがマクロで強制するのではなく、コンパイラに決定させる方が良いのではないでしょうか? "これにより、コンパイラはそれを使用するすべてのコンパイル単位で関数のコードを発行します。"しかし、そのファイルで使用されていなければ、コンパイルされません。ここにコンパイルされたコードサイズの制約があります。 – endolith

+1

"はい、コンパイラにマクロではなく強制的に決定させる方が良いのですか?"私はこの質問を理解していません。 「しかし、そのファイルで使われていなければ、コンパイルされませんよね?」コンパイルされますが、マシンコードは発行されません。それは本質的に「捨てられる」。 –

0

あなたからそれらを使用する場合は、ヘッダーファイルでそれらを配置する必要があります複数のファイル。

リンカエラーの場合:関数のデフォルト宣言は "extern"を意味しますが、インライン展開されているため、リンカはコンパイラ生成シンボルのスタブを見つけることができます。 C99で動作するようにinline機能については

1

(彼らは唯一の言語の中に存在してきた)あなたはヘッダファイルに

inline void do_something(void) 
{ 
    blah 
} 

1つのコンパイル単位(別名.Cでの定義を与える必要があるだろう)あなたは何らかの「インスタンス化」を行います

void do_something(void); 

inlineなし。

+1

合意。ここでも同様の質問があるようです。 http://stackoverflow.com/questions/6312597/is-inline-without-static-or-externever-useful-in-c99 –

関連する問題