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

  • SEARCH
  • Home
  • 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 8501125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:07:21+00:00 2026-06-11T01:07:21+00:00

I have 3 files to compile with G++, the main file is like this:

  • 0

I have 3 files to compile with G++, the main file is like this:

//main.cpp

#include "test.hpp"

int main(int argc,char** args) {
  //
}

The second file is the header file:

//test.hpp

namespace shared {
  class test {

    //constructor 
    test();
  };
}

The last file is the code file for test.hpp

//test.cpp

shared::test::test() {
  //
}

And I compile using G++ this way:

g++ -c main.cpp test.cpp

However, G++ complains about undefined identifier ‘shared’ in the file ‘test.cpp’. In the command line I already pass in file ‘main.cpp’, which includes the header file. How to fix this? I only want to have all the ‘#include’s be in main.cpp, and no where else.

  • 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-06-11T01:07:22+00:00Added an answer on June 11, 2026 at 1:07 am

    Add #include "test.hpp" at the beggining of test.cpp.

    Compiler doesn’t care about the order of files in the commandline. It only affects the linker.
    Please also note, that the usual way of compiling multi-file projects is to compile each of them to different sub-object like so:

    g++ main.cpp -o main.o
    g++ test.cpp -o test.o
    ld main.o test.o -o program[.exe]
    

    This allows you to recompile only the files that really did change. If you think about it for a while, you’ll find out that a .cpp file can contain many headers without a problem; however, the compilation time will increase when your headers will start to have many headers included. Forward declarations can help solve those issues, yet with your simple example simple solution will work.

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

Sidebar

Related Questions

This is my first attempt to compile a main.cpp file with cygwin. The command
I Have a LD_PRELOAD file. On what OS and conditions i should compile this
I have this source file: // ConstPointer.cpp const short * const const_short_p_const = 0;
I found this sample code on the msdn library #include <iostream> int main() {
Take the following simple source (name it test.cpp): #include <windows.h> void main() { DebugBreak();
I have several files which is causing a compile error, can the following be
I have three source files in a folder. I simply want to compile them
I'm trying to compile many files using the Compiler API. Say I have a
I have compiled my preload file on Ubuntu server (two files for x32 and
I have a java app, and the log4j.properties file is in src/com/my/path/props. On compile,

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.