CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: #defines

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    #defines

    Hey.

    I have a problem where I've #defined a macro so that something else will be defined:
    Code:
    #ifdef _DEBUG
    #define USE_LOG
    #endif
    
    #include <string>
    
    #include "SDL.h"
    #include "SDL_image.h"
    #include "SDL_ttf.h"
    
    #include "Audio_Engine.h"
    #include "Logger.h"
    #include "Settings.h"
    #include "Screen_Event_Manager.h"
    #include "SDL_System.h"
    
    #include "Earth.h"
    Logger is the file that I want to see USE_LOG as being defined because it "activates" a logging function if it is defined:
    Code:
    #ifdef USE_LOG
        #define LOG(string) Logger::Get().Log(string)
    #else
        #define LOG(string)
    #endif
    When I debug it, any LOG() macro call gets skipped. I simplified it down to this and it works:
    Code:
    #ifdef _DEBUG
    #define USE_LOG
    #endif
    
    #include "Logger.h"
    
    int main()
    {
        LOG("Logged something!");
    
        return 0;
    }
    Code:
    #ifndef LOGGER_H
    #define LOGGER_H
    
    #include <iostream>
    
    #ifdef USE_LOG
        #define LOG(string) std::cout << string << std::endl
    #else
        #define LOG(string)
    #endif
    
    #endif
    The Logger.h file (in the first example) is in a static library that I use. Would this affect it? What I am doing wrong?

    Cheers.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: #defines

    Are you sure _DEBUG is being properly defined when compiling in debug mode?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: #defines

    Quote Originally Posted by Marc G View Post
    Are you sure _DEBUG is being properly defined when compiling in debug mode?
    Hey. Yep, it is. See the attachment.
    Attached Images Attached Images  
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: #defines

    Hello Mybowlcut

    I really havn't tried this myself (except for the sample exercises in the book xD)
    maybe try the cpp thing? I don't know... well I tried.

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: #defines

    Weird ... Maybe one of the other includes is undefining USE_LOG again?
    I would enable the option to force the preprocessor to write the preprocessed result to a file. Then open that file and maybe that might give a clue what's going on.
    To enable that, right click the file in your solution explorer, click properties, go to C++ > Preprocessor and enable generate preprocessed file. It will create a file with extension .i which is just plain text.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: #defines

    I try to generate the preprocessed file but MS VS 2008 return an error like this

    fatal error LNK1104: cannot open file '.\Debug\Arcs.obj' Graph
    Thanks.
    Thanks for your help.

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: #defines

    Don't compile the whole project with this preprocessed file output enabled.
    Enable the preprocessed file output for the file you want to investigate. Then right click that file and click compile to only compile that one file. It should generate a .i file which you can open in any text editor.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    Oct 2006
    Posts
    616

    Re: #defines

    When you use this code instead of the library:
    Code:
    #ifndef LOGGER_H
    #define LOGGER_H
    
    #include <iostream>
    
    #ifdef USE_LOG
        #define LOG(string) std::cout << string << std::endl
    #else
        #define LOG(string)
    #endif
    
    #endif
    does it work with your original code ?
    If so, it might be a problem with the library or with how you use it - is the log file open for writing ? are the function calls correct ? etc.


    Regards,
    Zachm

  9. #9
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: #defines

    Quote Originally Posted by Marc G View Post
    Weird ... Maybe one of the other includes is undefining USE_LOG again?
    I would enable the option to force the preprocessor to write the preprocessed result to a file. Then open that file and maybe that might give a clue what's going on.
    To enable that, right click the file in your solution explorer, click properties, go to C++ > Preprocessor and enable generate preprocessed file. It will create a file with extension .i which is just plain text.
    Ok I did this and it's 52,000 lines long. Any clues as to what I'm looking for?

    Quote Originally Posted by Zachm View Post
    When you use this code instead of the library:
    Code:
    #ifndef LOGGER_H
    #define LOGGER_H
    
    #include <iostream>
    
    #ifdef USE_LOG
        #define LOG(string) std::cout << string << std::endl
    #else
        #define LOG(string)
    #endif
    
    #endif
    does it work with your original code ?
    If so, it might be a problem with the library or with how you use it - is the log file open for writing ? are the function calls correct ? etc.


    Regards,
    Zachm
    It does the same - nothing.. haha. Log file is there and will open and the function calls are correct. I'm 99&#37; positive that it's the #define that is wrong.
    Last edited by Mybowlcut; April 7th, 2009 at 10:10 AM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: #defines

    Yes, you're looking at the pre-processed output (output from the pre-processor). So, look for your replaced macro:
    Code:
    std::cout << string << std::endl
    Viggy

  11. #11
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: #defines

    50000 lines for one file? Geez I never knew so much was happening when I compile haha.

    I couldn't find the replaced macro in there. I'm trying to enable it for main.cpp in the project that uses the static library where Logger is but I get this link error:
    Code:
    fatal error LNK1104: cannot open file '.\Debug\main.obj'	Earth 1.2
    Whenever I don't use the macro to access the Logger singleton, it works fine:
    Code:
    Logger::Get().Log("oooooook.");
    
        Logger::Get().Save_Log();
    Those both work.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  12. #12
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: #defines

    Maybe, your static library is compiled without _DEBUG defined? Try to recompile all the stuff of your project making sure _DEBUG is defined for each file. You can reach this by doing -D_DEBUG command line compiler option.

  13. #13
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: #defines

    Quote Originally Posted by Mybowlcut View Post
    50000 lines for one file? Geez I never knew so much was happening when I compile haha.

    I couldn't find the replaced macro in there.
    Then try to find the place in the code where the logger line should have been and see what the preprocessor made of it. Also find the place where you had your include<logger.h> and see what exactly got included.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  14. #14
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: #defines

    I forgot about this! I have attached a zip file containing a file's processor output that uses Logger.h. I found where the class declaration for Logger is (just going to post a snippet) and it doesn't seem that the #define in Logger.h is working:
    Code:
    #line 37 "c:\\documents and settings\\bill\\my documents\\visual studio 2005\\projects\\sdl_game_engine_1_1\\sdl_game_engine_1_1\\time.h"
    #line 7 "c:\\documents and settings\\bill\\my documents\\visual studio 2005\\projects\\sdl_game_engine_1_1\\sdl_game_engine_1_1\\logger.h"
    
    
    
    
    
        
    #line 14 "c:\\documents and settings\\bill\\my documents\\visual studio 2005\\projects\\sdl_game_engine_1_1\\sdl_game_engine_1_1\\logger.h"
    
    
    
    class Logger
    {
    public:
    	static void Initialise(const std::string& file_name);
    This is what the actual code for the above looks like:
    Code:
    #include <time.h>
    #include <vector>
    
    #ifdef USE_LOG
        #define LOG(string) Logger::Get().Log(string)
    #else
        #define LOG(string)
    #endif
    
    // Singleton designed to log application and game details to
    // aid in difficult bugs and errors.
    class Logger
    {
    public:
      static void Initialise(const std::string& file_name);
    The LOG macro was actually called by the code in the project that uses the project that Logger is in but this is what the processor thought of it:
    Code:
    void Settings::Save()
    {
        if(!initialised)
    	{ 
    		throw LRE_Exception("Settings not initialised.");
    	}
    
        instance.Write(instance.file_name);
    
        ;
    }
    Just a semicolon! Hope VS is optimizing that away for me.

    Here is where Settings.cpp includes Logger.h in my code:
    Code:
    #include "stdafx.h"
    
    #include "Settings.h"
    
    #include <exception>
    
    #include "include_ticpp.h"
    
    #include "Logger.h"
    
    Settings Settings::instance = Settings();
    bool Settings::initialised = false;
    
    Settings::Settings()
    {
    }
    
    Settings::~Settings()
    {
    }
    
    // ...
    So what's going wrong? _DEBUG macro not working? _DEBUG is in my preprocessor options in both projects (as it should be by default for debug configurations). I think it's not seeing the USE_LOG define:
    Code:
    #ifdef _DEBUG
    #include "vld.h"
    #define USE_LOG
    #endif
    "vld.h" was included, but I CTRL+Fed for USE_LOG and it didn't find it... or does it not output defines that don't define anthing?
    Attached Files Attached Files
    Last edited by Mybowlcut; April 13th, 2009 at 10:51 AM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  15. #15
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: #defines

    Have you tried to "probe" the defines?
    Code:
    #ifdef _DEBUG
    #error
    #endif
    Insert it "everywhere", before and after including Logger.h for instance. Try even thing you _know_ shouldn't be a problem like LOGGER_H, USE_LOG and so on. Macros can provide such an headache...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured