Thread: Using Too Many Preprocessor Directives

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    Question Using Too Many Preprocessor Directives

    I am wrapping up at my college and will be going into game programming. So, I am trying to develop good habits now on personal projects instead of being told that I am a bad coder by my manager when I am in the workplace.

    I had a poster replaying to one of my threads saying that the use of preprocessor directives in header files can lead to issues, or using them when you don't need them can be wasteful. Is this true?

    Do I really need to take the time to remove the ones that I do not need on the files that I do not need them? Or can I leave them as is and move onto bigger problems?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by c_lover View Post
    I had a poster replaying to one of my threads saying that the use of preprocessor directives in header files can lead to issues, or using them when you don't need them can be wasteful. Is this true?
    Yep, it can lead to ODR violations.
    It also leads to more complicated and harder to read code.

    Do I really need to take the time to remove the ones that I do not need on the files that I do not need them? Or can I leave them as is and move onto bigger problems?
    Well, that's up to you. But if you want to practice reducing them, I'd suggest getting rid of the ones that are there.
    Restructure your code so that you remove unnecessary ones and if they are necessary and it is possible, put them into source files instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

    Jim

  4. #4
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    Post

    Quote Originally Posted by Elysia View Post
    Yep, it can lead to ODR violations.
    It also leads to more complicated and harder to read code.


    Well, that's up to you. But if you want to practice reducing them, I'd suggest getting rid of the ones that are there.
    Restructure your code so that you remove unnecessary ones and if they are necessary and it is possible, put them into source files instead.
    Ok, so what you are saying is that I can just copy and paste my lines of #include into each of my source files, but omit them from the header files?

    E.g. I can put the following in each of my source and I am good to go?

    // PREPROCESSOR DIRECTIVES
    #include <iostream>
    #include <string>
    #include <vector>
    #include <stdlib.h>
    #include <algorithm>
    #include <array>
    #include <time.h>
    #include <map>
    #include <cstring>
    #include <cctype>
    #include <sstream>
    #include <math.h>
    #include <limits>
    #include <SFML/System.hpp>
    #include <SFML/Window.hpp>
    #include <SFML/Graphics.hpp>
    #include <SFML/Audio.hpp>
    #include <SFML/Network.hpp>
    #include "Shape.h"
    #include "Player.h"
    #include "Cursor.h"
    #include "Interface.h"
    #include "Gameplay.h"
    #include "Tutorial.h"

    If not, then I do not understand the link you provided me because I copied/pasted the above into all my files (.cpp and .h) and I do not have any compiler errors..??
    Last edited by c_lover; 07-11-2015 at 08:38 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Put whatever #includes your headers need into your headers. Forward declare if you can. Avoid other preprocessor pragmas in headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    You think that's alot? I have a 38000 codeline library, 10% of that is preprocesser code... So many macros it is not fun to refactor.

  7. #7
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Quote Originally Posted by Elysia View Post
    Put whatever #includes your headers need into your headers. Forward declare if you can. Avoid other preprocessor pragmas in headers.
    Ok, so if I remove a certain #include line, and it was actually needed somewhere in my program, then it will tell me at compile time rather than when the function that needed it is called, right?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by c_lover View Post
    ...then it will tell me at compile time...
    Yes, obviously.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Preprocessor Directives
    By Atomic_Sheep in forum C++ Programming
    Replies: 2
    Last Post: 06-26-2011, 03:48 AM
  2. Preprocessor directives
    By Sonia in forum C Programming
    Replies: 2
    Last Post: 10-28-2010, 05:29 AM
  3. Preprocessor Directives
    By Sentral in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 10:09 AM
  4. \ in preprocessor directives
    By shawn_drn in forum C Programming
    Replies: 1
    Last Post: 01-16-2005, 03:49 PM
  5. preprocessor directives
    By linuxdude in forum C Programming
    Replies: 1
    Last Post: 12-16-2003, 10:30 PM

Tags for this Thread