Conditional Compilation

Feb 6, 2017 at 1:59am
Hi all,

I was trying something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, char** argv) {
	uint numQueens = 8;
	if(argc > 1) {
		numQueens = atoi(argv[1]);
		if(argc > 2) {
			#define DEBUG
		} // END if
	} // END if

	cout << numQueens << '\n';

	#ifdef DEBUG
		Board myBoard(numQueens, true);
	#else
		Board myBoard(numQueens);
	#endif

return 0;
} // END main(argc, argv) 


When I realized DEBUG would always be #define'd since it is a preprocessor directive. My question is, is there a simple way to do what I'm trying by passing an argument to the execution command as opposed to just adding #define DEBUG in the actual source?
Last edited on Feb 6, 2017 at 2:00am
Feb 6, 2017 at 2:08am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Board createBoard(uint nQueens, bool val) {
    if ( val ) return Board(nQueens, true);
    return Board(nQueens);
}

int main(int argc, char** argv) {
	uint numQueens = 8;
        bool debugFlag = false;
	if(argc > 1) {
		numQueens = atoi(argv[1]);
                debugFlag = argc > 2;
	} // END if

	cout << numQueens << '\n';

        Board myBoard = createBoard(numQueens, debugFlag);

        // or, since myBoard(numQueens) is probably a shortcut
        // for myBoard(numQueens, false), just:
        // Board myBoard(numQueens, debugFlag); 

}
Feb 6, 2017 at 2:18am
Is there a way to do it that would maintain the conditional compilation? (No reason I actually need it just thought it would be a cool thing to learn how to do if its possible)

And you're correct, Board is a class I created and was trying the same concept (lines 5-7 of OP, except with the second arg for the Ctor) for debug print statements within it.
Feb 6, 2017 at 2:23am
Is there a way to do it that would maintain the conditional compilation?

No. There is no way to use run-time information to affect compilation which has to happen before the program is run.
Feb 6, 2017 at 2:30am
Right, that totally makes sense now that I think about it (duh).

Do you happen to know if there is a flag you can pass g++/gcc that will set a debug macro?

I'm looking at https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html right now but this seems more like dump info for gdb (?)
Feb 6, 2017 at 2:33am
Seen as you want to debug, why not just use a debugger (like gdb) or the one built-in your IDE?
Feb 6, 2017 at 2:34am
Feb 6, 2017 at 2:44am
@TheIdeasMan
That's definitely a good idea but as is learning gdb seems like a whole project of it's own (although certainly worthwhile in the future)

@cire
Sweet =D That's exactly what I was looking for.

g++ -D DEBUG main.cpp -o OUT


Will set a macro named DEBUG (or whatever you put after the -D flag)
Feb 6, 2017 at 3:02am
That's definitely a good idea but as is learning gdb seems like a whole project of it's own (although certainly worthwhile in the future)


And the GUI debugger in your IDE? Even learning gdb from the shell is not as hard as you think it is.

It just seems to me that putting all that into your code is messy, and is a kind of "poor man's debugging"

Feb 6, 2017 at 3:10am
As far as I can tell the only GUI I have is just an extension to GDB, the -tui flag.

This being said, if you have any recommendations for a GUI-based free debugger for Ubuntu I would certainly welcome them =D

SO seems to recommend DDD, Nemiver, and Eclipse CDT although I've no clue the differences between them.
Feb 6, 2017 at 3:28am
Are you using an IDE? If so, there should be one built in.

I use QtCreator or KDevelop, they both have very easy to use GUI interfaces to the debugger.
Feb 6, 2017 at 4:30am
Generally I just use VS as my IDE, but it's on my desktop and I'm currently not able to access it.

My laptop runs Ubuntu and I just edit files in Sublime then compile w/g++, although I rarely use my laptop as a programming environment. Frankly, I'm new to Ubuntu and once I found and figured out how to install something that worked I just left it at that and haven't revisited the topic until now.

I will check out both of those, thank you for the recommendations!
Feb 6, 2017 at 5:43am
> Generally I just use VS as my IDE

Strongly favour debugging all portable C++ code with the integrated debugger in Visual Studio; it is a debugger that understands C++.

For instance; try this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <memory>
#include <string>
#include <cstdio>

// for demo purposes; could have directly used the deleter &std::close
void close_it( std::FILE* file ) { std::fclose( file ) ; }


int main()
{
    auto p = std::make_shared<std::string>( "hello world" ) ;
    auto p2 = p ;
    std::weak_ptr<std::string> wp(p) ;
    // examine p in the Visual Studio debugger by placing the cursor over it
    // it would report:
    // p shared_ptr "hello world" [2 strong refs, 1 weak ref] [make_shared] std::shared_ptr< std::basic_string<char...

    std::shared_ptr<std::FILE> file( std::fopen( __FILE__, "r" ), &close_it ) ;
    // examine file in the debugger by placing the cursor over it
    // it would report:
    // file shared_ptr {_Placeholder=0x0 } [1 strong ref] [custom deleter] std::shared_ptr<_iobuf>
    // pull it down, and the deleter would be shown as:
    // [deleter] <address> {test1.exe!close_it(struct _iobuf *)} std::_Compressed_pair<...
}
Feb 6, 2017 at 5:57am
closed account (E0p9LyTq)
Generally I just use VS as my IDE, but it's on my desktop and I'm currently not able to access it.


Visual Studio Code can run on Ubuntu, so you still get integrated VS debugging with a more lightweight IDE.
Feb 6, 2017 at 6:32am
@JLBorges
I'll have to check this out whenever I get access to VS again (hopefully soon if I can figure out how to download VS Code via FurryGuys' post)

@FurryGuy
Whoa... Last I checked VS was only available via Wine on Ubuntu, and (as is the case with most things running through Wine that I've checked out) was quite buggy. A quick Google search proves me wrong, so I'll have to look into this! Is this only available for VS Code or the whole VS suite? I assume this excludes .NET but my experience w/C# (and anything in .NET) is rather limited.
Last edited on Feb 6, 2017 at 6:36am
Feb 6, 2017 at 7:04am
> I'll have to check this out whenever I get access to VS again
> (hopefully soon if I can figure out how to download VS Code via FurryGuys' post)

You will have to check it out on windows.
VSCode uses GDB or LLDB as the debugging back-end; don't think that it has the complete set of debug visualisers.
Feb 6, 2017 at 7:11am
closed account (E0p9LyTq)
@edge6768,

Visual Studio Code, for whatever platform, is designed to be very lightweight. Just the ISO compliant language and nothing else.

Cross-platform with the MS .NET junk should be declared a crime against humanity. It is suited for Windows, not *nix.

Visual Studio Code is as close to ISO standard C++ as possible with MS. Add in the debugger and it is a almost perfect.

MS is really taking to heart the philosophy of cross-platform with a vengeance.
Feb 6, 2017 at 8:28am
Visualising components in the debugger is all about the debugger being aware of and understanding the implementation details of a compiler and the associated standard library.

As such, a C++ debugger does not require knowledge of 'ISO standard C++' . The standard allows a variety of different implementations; the debugger needs to know the specific implementation details.

For instance, the Visual Studio debugger works equally well with the LLVM front-end 'Clang With Microsoft CodeGen'. In other words the front-end deals with 'ISO standard C++' (in this particular case clang++ does), the debugger deals with the back-end (Microsoft CodeGen).
Topic archived. No new replies allowed.