include files that are'nt in compiler's directory

This is a discussion on include files that are'nt in compiler's directory within the C++ Programming forums, part of the General Programming Boards category; How can i include files that are not in compiler's directory. If i write Code: #include<iostream.h> or#include<folder\innerfolder\file.cpp> Then this file ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    include files that are'nt in compiler's directory

    How can i include files that are not in compiler's directory.
    If i write
    Code:
    #include<iostream.h>
    or#include<folder\\innerfolder\\file.cpp>
    Then this file refers to the path
     c:\\tc\\folder\\innerfolder\\file.cpp
    Code:
    How to include a c:\\anyfolder\\anyfile.cpp???

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    #include"c:\anyfolder\anyfile.h"
    or
    #include<c:\anyfolder\anyfile.h>
    Last edited by qqqqxxxx; 03-23-2006 at 04:35 AM.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    use quotes when you're including your own headers... and use relative paths...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Oh, and since it wasn't mentioned... I couldn't help but notice you had:
    Code:
    #include <suchandsuch.cpp>
    You don't include CPP files, you only include libraries. Source code is compiled into object files which is then linked to create the executable. Libraries are combined with your source code by the preprocessor before compilation.

    Not that your compiler won't accept it, it's just standard practice.
    Last edited by SlyMaelstrom; 03-23-2006 at 05:02 AM.
    Sent from my iPadŽ

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Local header files you include with
    #include "header.h"

    Your own library headers, or library headers for 3rd party libraries you've downloaded say
    #include <library.h>

    System header files
    #include <iostream>

    To compile the code, you would do something like
    gcc -I/path/to/library prog.cpp

    Where /path/to/library is the directory where library.h is located.

    Of course, if you're using an IDE of some sort, you specify /path/to/library using some dialog or other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    My problem is sorted now.But I am left with some doubts.
    Why we are using
    Code:
    #include"c:\anyfolder\anyfile.cpp"
    instead of
    #include"c:\\anyfolder\\anyfile.cpp"
    quote:
    Code:
    You don't include CPP files, you only include libraries
    Here I am having one file say time.cpp whose object i want to declare in other file.That's why i am including time file.Is there any other clean or more acceptable method?

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by vaibhav
    Here I am having one file say time.cpp whose object i want to declare in other file.That's why i am including time file.Is there any other clean or more acceptable method?
    Yes, the proper method is to define the class in a header library, implement the class in the second file and include the header in both the main file and the implementation. Add the both to a project, compile and link.
    Code:
    // classProto.h
    
    #ifndef CLASS_PROTO_H
    #define CLASS_PROTO_H
    
    class foo {
       public:
          foo(int);
    
          int getData() const;
          void setData(int);
       private:
          int Data;
    };
    
    #endif
    Code:
    // ClassImplement.cpp
    
    #include "classProto.h"
    
    foo::foo(int DATA = 0) {
       Data = DATA;
    }
    
    int foo::getData() const {
       return Data;
    }
    
    void foo::setData(int DATA) {
       Data = DATA;
    }
    Code:
    // main.cpp
    
    #include <iostream>
    #include "classProto.h"
    
    int main() {
       foo bar(2);
       bar.setData(5);
       std::cout << bar.getData();
       return 0;
    }
    Sent from my iPadŽ

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Why we are using
    Code:
    #include"c:\anyfolder\anyfile.cpp"
    instead of
    Code:
    #include"c:\\anyfolder\\anyfile.cpp"
    It's best just to use forward slashes and forget about backslashes.
    Code:
    #include "c:/anyfolder/anyfile.h"
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Doesn't front slash(/) refers to web address only?

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    pssst:
    Code:
    jshao@MCP ~/Programming/C++ $
    here's a hint:
    Code:
    jshao@MCP ~/Programming/C++ $ uname
    Linux
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Doesn't front slash(/) refers to web address only?
    Forward slashes are Linux and Unix's directory separators. The original web servers were Unix ones, so the web evolved around forward slash directory separators.

    That is, in Windows, a path looks like this:
    Code:
    C:\windows\command.com
    But on *NIX, a path looks like this:
    Code:
    /sbin/ifconfig
    Most C functions, like remove, can take either forward slashes or backwards ones:
    Code:
    remove("..\\something");
    remove("../something_else");
    (C++ functions are the same way.) I actually prefer to use forward slashes, because you don't have to escape them.

    For #includes, having a backslash is undefined behaviour. Sometimes "..\h" works, sometimes "..\\h", sometimes neither.

    All told, it's best to just use forward slashes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uniting all .h files in an include folder
    By afflictedd2 in forum C++ Programming
    Replies: 8
    Last Post: 11-26-2008, 12:36 PM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 10:37 PM
  3. Help Required!!!!!
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 11-27-2005, 02:56 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Read and write hanging
    By zee in forum C Programming
    Replies: 8
    Last Post: 08-03-2004, 11:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21