Please read the new rule regarding the restriction on the use of AI tools. ×

Using custom macros. Get rid of ifdef #ifndef for ONLINE_JUDGE

Revision en1, by devildeweller, 2023-11-16 11:19:07

Many of you have used a debug tool or want to run some code locally but don't want it to run on other devices or platforms. Mainly competitive programmers use something like this -> ~~~~~

ifndef ONLINE_JUDGE

include "debug.h"

else

define dbg(...) ;

define debug(...) ;

define crndl ;

endif


and it works for online platforms that have defined online judges. But what if we want to run code on other websites without defined online judges or devices without removing the code every time? I face this problem and it is annoying. Before sharing code you have to remove some of the lines of the codes. Now, I recently came across the solution by defining your own macro you do not have to think while submitting code or sharing your code. Instead of using `#ifndef ONLINE_JUDGE` you can use `ifdef (any_name)`. EX->

ifdef Loki

include "debug.h"

else

define dbg(...) ;

define debug(...) ;

define crndl ;

endif


Now to run this code in your device you have two options either running by command or changing the build system. Command-> ` g++ -DLoki ex.cpp -o ex` Basically you have to add `-D(name_you_used)` after g++. For Linux build system ->

{ "cmd" : ["g++ -DLoki -std=c++17 $file_name -o $file_base_name && timeout 5s ./$file_base_name<inputf.in>outputf.in"], "selector" : "source.c", "shell": true, "working_dir" : "$file_path" } ~~~~~

For Windows build system -> ~~~~~ { "cmd": ["g++.exe", "-DLoki", "-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&", "${file_base_name}.exe<inputf.in>outputf.out"], "shell":true, "working_dir":"$file_path", "selector":"source.cpp" } ~~~~~

For Mac build system -> ~~~~~ { "cmd" : ["g++-DLoki -10 $file_name -o $file_base_name && gtimeout 4s ./$file_base_name<inputf.in>outputf.in"], "selector" : "source.c", "shell": true, "working_dir" : "$file_path" } ~~~~~ Hope this will help you. Have a great day.

Tags preprocessor, c++, macros, local, ifdef, ifndef, build system, windows, linux, macos, sublime text setup, debug

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English devildeweller 2023-11-16 11:30:26 109 Tiny change: ' this ->\n```\n#ifndef ' -> ' this ->\n\n```c++\n#ifndef ' (published)
en1 English devildeweller 2023-11-16 11:19:07 2080 Initial revision (saved to drafts)