Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ simple files compilation using "gcc" compiler
    primarykey
    data
    text
    <p>I'm trying to compile a very simple c++ code and get a linking error depending on sequence of gcc parameters. Could anybody explains what is the difference between these two command line rows?</p> <p>// This works fine</p> <pre><code>gcc -x c++ -c *.h *.cpp (first I pass *.h files then *.cpp) gcc -lstdc++ *.o -o exe </code></pre> <p>// This gives an error</p> <pre><code>gcc -x c++ -c *.cpp *.h (first I pass *.cpp files then *.h) gcc -lstdc++ *.o -o exe </code></pre> <p>Does gcc care of the parameters sequence? </p> <p>Examples:</p> <p>// THIS CASE WORKS FINE</p> <pre> [karen@linux40 ~/C++]$ ls Employee.cpp Employee.h Main.cpp [karen@linux40 ~/C++]$ gcc -x c++ -c *.h *.cpp [karen@linux40 ~/C++]$ ls Employee.cpp Employee.h Employee.o Main.cpp Main.o Makefile [karen@linux40 ~/C++]$ gcc -lstdc++ *.o -o exe [karen@linux40 ~/C++]$ ls exe Employee.cpp Employee.h Employee.o Main.cpp Main.o </pre> <p>// THIS IS THE PROBLEMATIC CASE</p> <pre> [karen@linux40 ~/C++]$ ls Employee.cpp Employee.h Main.cpp [karen@linux40 ~/C++]$ gcc -x c++ -c *.cpp *.h [karen@linux40 ~/C++]$ ls Employee.cpp Employee.h Employee.o Main.cpp Main.o [karen@linux40 ~/C++]$ gcc -lstdc++ *.o -o exe Main.o: In function `main': Main.cpp:(.text+0x8d): undefined reference to `Employee::Employee()' collect2: ld returned 1 exit status </pre> <p>Employee.h</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; class Employee { public: Employee(); Employee(string theName, float thePayRate); string getName() const; float getPayRate() const; float pay(float hoursWorked) const; protected: string name; float payRate; }; </code></pre> <p>Employee.cpp</p> <pre><code>#include "Employee.h" Employee::Employee() { } Employee::Employee(string theName, float thePayRate) { name = theName; payRate = thePayRate; } string Employee::getName() const { return name; } float Employee::getPayRate() const { return payRate; } float Employee::pay(float hoursWorked) const { return hoursWorked * payRate; } </code></pre> <p>Main.cpp</p> <pre><code>#include "Employee.h" int main() { Employee e; return 0; } </code></pre>
    singulars
    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