Note that there are some explanatory texts on larger screens.

plurals
  1. POunspecified ouput using inline function in c++
    primarykey
    data
    text
    <p>I was playing with code to understand internal and external linkage in c++.I came up with the code whose out seems to vary depending on the sequence in which it is linked.</p> <p><br><strong>test1.cpp</strong> <br/></p> <pre><code>#include&lt;iostream&gt; using namespace std; inline int c() { static int p=0; p++; return p; } void a() { cout&lt;&lt;"\nIn function a() , c = "&lt;&lt;c(); } </code></pre> <p><br><strong>test2.cpp</strong><br/></p> <pre><code>#include&lt;iostream&gt; using namespace std; inline int c() { static int p=12; p++; return p; } void b() { cout&lt;&lt;"\nIn function b() , c = "&lt;&lt;c(); } </code></pre> <p><br><strong>driver.cpp</strong><br/></p> <pre><code>#include&lt;iostream&gt; using namespace std; void a(); void b(); int c(); int main() { b(); a(); a(); b(); cout&lt;&lt;"\nIn function main() = , c "&lt;&lt; c(); cout&lt;&lt;"\n"; } </code></pre> <p><strong>output 1 :-</strong> </p> <pre><code>when compiles as follows :- bash#&gt;g++ -c test1.cpp bash#&gt;g++ -c test2.cpp bash#&gt;g++ -c driver.cpp bash#&gt;g++ -o out driver.o test1.o test2.o bash#&gt;./out In function b() , c = 1 In function a() , c = 2 In function a() , c = 3 In function b() , c = 4 IN main() , c = 5 </code></pre> <p>In above output , compiler is considering c() defined in test1.cpp</p> <p><strong>output 2:-</strong> changing sequence of test1.o and test2.o while linking.</p> <pre><code>bash#&gt;g++ -o out driver.o test2.o test1.o In function b() , c = 13 In function a() , c = 14 In function a() , c = 15 In function b() , c = 16 IN main() , c = 17 </code></pre> <p>In above output , compiler is considering c() defined in test2.cpp</p> <p>I was perplexed when i made minor changes in the code , which are as follows :- <br>1) if I do not call c() in function a() [test1.cpp] and c() in funciton b()[test2.cpp]<br/></p> <pre><code>//test1.cpp changes void a() { cout&lt;&lt;"\nIn function a() , c = "; // not calling c() } //test2.cpp changes void b() { cout&lt;&lt;"\nIn function b() , c = "; // not calling c() } </code></pre> <p>I get following error while linking :-</p> <pre><code>bash#&gt;g++ -o out driver.o test1.o test2.o driver.o: In function `main': driver.cpp:(.text+0x1f): undefined reference to `c()' collect2: ld returned 1 exit status </code></pre> <p>2) If i call c() in any one of the file i.e either in test1.cpp or in test2.cpp , then i wont get linker error.</p> <p>Could anyone please help me in understanding this behaviour.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload