Thread: #INCLUDE Question about defining a preprocessor variable to hold a file path?

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    4

    #INCLUDE Question about defining a preprocessor variable to hold a file path?

    I have a bunch of files I want to include in my C code. The problem is that they are in a long directory path. They work, but it makes for an ugly source code.

    Example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    So far so good. And now for my special headers:

    #include <ridiculously/long/directory/path/that/points/to/my/special/header/files/header_1.h>
    #include <ridiculously/long/directory/path/that/points/to/my/special/header/files/header_2.h>
    #include <ridiculously/long/directory/path/that/points/to/my/special/header/files/header_3.h>
    #include <ridiculously/long/directory/path/that/points/to/my/special/header/files/header_4.h>
    #include <ridiculously/long/directory/path/that/points/to/my/special/header/files/header_5.h>

    The above actually works but it sure is messy.

    My Question:

    Is there any way to define a file path in the preprocessor section so I can clean this up so it appears as something like the following:


    path = "ridiculously/long/directory/path/that/points/to/my/special/header/files"

    #include <path/header_1.h>
    #include <path/header_2.h>
    #include <path/header_3.h>
    #include <path/header_4.h>
    #include <path/header_5.h>

    Note: I realize that I could move all these special header files to the standard header directory path, but there are reasons I don't want to do that.

    So is there any way to clean this up in the preprocessor section of the source code document?

    Thanks

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    776
    Usually, you would tell your compiler about the include directory. In GCC for instance, it might look like gcc -I/ridiculously/long/path source.c -o program

    And in the code just:
    Code:
    #include <stdio.h>
    
    #include "header1.h"
    #include "header2.h"
    #include "subfolder/special_header.h"
    
    ...

  3. #3
    Registered User
    Join Date
    Mar 2018
    Posts
    4
    I see what you are saying. I was trying to do it from the IDE. I never thought of adding a path to the actual compiler. However, I'm trying that and it doesn't seem to want to add the path. I must be doing something wrong. Is there a option command for gcc that will list out all its current search paths? When I try to add the new path it says gcc: fatal error: no input files. Compilation terminated.

    But I'm not trying to compile anything here on the command line. I just want to add a search directory path. I do the actual compiling using Geany IDE. All I want to do with gcc is tell it where it can find the files. Geany then calls the gcc when I click on "Compile" or "Build" in the IDE.

    It actually works if I hard-code the file path in the #include statement. So I know the program works. It just need it to find my header files without having to type in the path.

    Is there a way to list the current search paths from the command line using gcc?

    I just want to add another search path. I don't want to actually compile anything from the command line.

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    776
    I've never used Geany, but judging by screenshots, it looks like you can set the properties in the GUI.

    So you would add the -I/your/path/to/headers part to the Compile Command (field) and that should hopefully automate things for you. I don't know if this can be done on a per-project basis.

  5. #5
    Registered User
    Join Date
    Mar 2018
    Posts
    4
    I've been trying to set the file path in that window Build Properties window with no luck. Some of the directory names in my path include spaces between words. So maybe I need to include quotes around those specific directory names. Things get nasty when directory paths get complicated. But it seems to work ok when I hard-code it into the actual include statement. So at least I know it works that way. I keep messing with it.

    Thanks for the quick reply.

  6. #6
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    776
    Some of the directory names in my path include spaces between words. So maybe I need to include quotes around those specific directory names.
    Yes, that's exactly what you can do: -I"/your/path/to/headers"

    Directory names shouldn't be a show stopper.

    But it seems to work ok when I hard-code it into the actual include statement. So at least I know it works that way. I keep messing with it.
    That's a bit of a hack though. Since I assume you want to get more proficient in your coding experience, you should try to adopt reliable practices. Nothing wrong with using the command line btw. The only advantage on an IDE is intelligent tooling -- beyond that it only obfuscates what's going on.

  7. #7
    Registered User
    Join Date
    Mar 2018
    Posts
    4
    I found the answer!

    I never needed to mess with paths at all. All I needed to do was place my filenames in quotes instead of in brackets like so,...

    Code:
    // System includes:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // My special project includes:
    #include "myfile.h"
    #include "myfile.c"
    As long as these files are in my working directory I don't need a path. And if I want to put them in a sub directory I only need to add the name of that single directory as long as it's inside the working directory.

    This is what I wanted.

    It all about just using quotes instead of brackets in the include statement. No need to mess with defining the paths as long as these files are in the working directory of the current project, which in my case they are.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to hold Variable value upto certain limit
    By Mine in forum C Programming
    Replies: 1
    Last Post: 10-17-2016, 06:59 PM
  2. include path
    By Mahesh Jagtap in forum Linux Programming
    Replies: 1
    Last Post: 09-16-2012, 08:24 AM
  3. The Preprocessor; Using the #include statement
    By tiger6425 in forum C Programming
    Replies: 8
    Last Post: 08-14-2010, 01:42 AM
  4. Replies: 6
    Last Post: 06-24-2010, 11:12 PM
  5. Can't hold numbers in variable.
    By xamlit in forum C Programming
    Replies: 10
    Last Post: 12-02-2005, 05:05 PM

Tags for this Thread