Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:10:18+00:00 2026-05-31T17:10:18+00:00

I’m trying to compile a very simple c++ code and get a linking error

  • 0

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?

// This works fine

gcc -x c++ -c *.h *.cpp (first I pass *.h files then *.cpp)
gcc -lstdc++ *.o -o exe

// This gives an error

gcc -x c++ -c *.cpp *.h (first I pass *.cpp files then *.h) 
gcc -lstdc++ *.o -o exe

Does gcc care of the parameters sequence?

Examples:

// THIS CASE WORKS FINE


    [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

// THIS IS THE PROBLEMATIC CASE


    [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

Employee.h

#include <iostream>
#include <string>
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;
};

Employee.cpp

#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;
}

Main.cpp

#include "Employee.h"

int main() {
    Employee e;
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:10:19+00:00Added an answer on May 31, 2026 at 5:10 pm

    Generally you should not pass .h file directly to compiler. That does not make any sense and is not correct.

    But looks like the question here is mainly for understanding how gcc works. So my answer is from this perspective.

    I assume you include the same .h file in .cpp file.

    What happens when you pass the .cpp first then .h, the compiler sees a declaration, then definition and then again a declaration. This causes a linker error, as there is no corresponding definition for the second declaration.

    In contrast if you pass the .h first then .cpp, it sees two declarations and then the definition, which links fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.