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 8661655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:32:59+00:00 2026-06-12T16:32:59+00:00

I have a project (a library) that is subdivided into a few directories with

  • 0

I have a project (a library) that is subdivided into a few directories with code in them. I’d like to to have g++ search for header files in the project’s root directory, so I can avoid different include paths for same header files across multiple source files.

Mainly, the root/ directory has sub-directories A/, B/ and C/, all of which have .hpp and .cpp files inside. If some source file in A wanted to include file.hpp, which was in B, it would have to do it like this: #include "../B/file.hpp". Same for another source file that was in C. But, if A itself had sub-directories with files that needed file.hpp, then, it would be inconsistent and would cause errors if I decided to move files (because the include path would be "../../B/file.hpp").

Also, this would need to work from other projects as well, which reside outside of root/. I already know that there is an option to manually copy all my header files into a default-search directory, but I’d like to do this the way I described.

Edit: all programs using the library must compile only with g++ prog.cpp lib.a -o prog. That means permanently setting the include path for g++!

  • 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-12T16:33:00+00:00Added an answer on June 12, 2026 at 4:33 pm

    A/code.cpp

    #include <B/file.hpp>
    

    A/a/code2.cpp

    #include <B/file.hpp>
    

    Compile using:

    g++ -I /your/source/root /your/source/root/A/code.cpp
    g++ -I /your/source/root /your/source/root/A/a/code2.cpp
    

    Edit:

    You can use environment variables to change the path g++ looks for header files. From man page:

    Some additional environments variables affect the behavior of the
    preprocessor.

       CPATH
       C_INCLUDE_PATH
       CPLUS_INCLUDE_PATH
       OBJC_INCLUDE_PATH
    

    Each variable’s value is a list of directories separated by a special character, much like PATH, in which to look for header
    files. The special character, “PATH_SEPARATOR”, is target-dependent and determined at GCC build time. For Microsoft Windows-based targets it
    is a semicolon, and for almost all other targets it is a colon.

    CPATH specifies a list of directories to be searched as if specified with -I, but after any paths given with -I options on the
    command line. This
    environment variable is used regardless of which language is being preprocessed.

    The remaining environment variables apply only when preprocessing the particular language indicated. Each specifies a
    list of directories to be
    searched as if specified with -isystem, but after any paths given with -isystem options on the command line.

    In all these variables, an empty element instructs the compiler to search its current working directory. Empty elements can
    appear at the beginning
    or end of a path. For instance, if the value of CPATH is “:/special/include”, that has the same effect as -I.
    -I/special/include.

    There are many ways you can change an environment variable. On bash prompt you can do this:

    $ export CPATH=/your/source/root
    $ g++ /your/source/root/A/code.cpp
    $ g++ /your/source/root/A/a/code2.cpp
    

    You can of course add this in your Makefile etc.

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

Sidebar

Related Questions

We have a Library Project that we use for all our central reused code
I have a class, which is part of a code library project that was
I have created a Library Project which I import into another project. In that
I have a library project that uses a jar (httpmime-4.1.3.jar). I'd like to take
The situation: We have a library project that houses much of our code for
Suppose I have a project ( myproject - a library that builds into a
I have a dot net 4.0 DAL library project that contains three edmx files.
I have a Qt project and would like to use an external library that
I have a library project that should be the base for several web applications.
Good day, I have a library project that i use to create a paid

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.