# Is there a feature like C's #define in order to compile or not e.g. println

**URL:** <https://discourse.julialang.org/t/is-there-a-feature-like-cs-define-in-order-to-compile-or-not-e-g-println/123542>\
**Category:** General Usage\
**Created:** [December 6, 2024, 2:51pm UTC](https://discourse.julialang.org/t/is-there-a-feature-like-cs-define-in-order-to-compile-or-not-e-g-println/123542 "2024-12-06T14:51:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [December 6, 2024, 2:51pm UTC](https://discourse.julialang.org/t/is-there-a-feature-like-cs-define-in-order-to-compile-or-not-e-g-println/123542/1 "2024-12-06T14:51:57Z")

</div>

Hi,

I would like to activate/deactivate integration of some println in my compiled code.  
I guess I could set a debug constant and fill the code with:

```julia
const debug = 0

if debug == 1
    println("value = " * value)
end

```

I suppose that it won’t be compiled in (am I right ?).

But maybe there’s some Julia’s way to do that ?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 6, 2024, 2:59pm UTC](https://discourse.julialang.org/t/is-there-a-feature-like-cs-define-in-order-to-compile-or-not-e-g-println/123542/2 "2024-12-06T14:59:05Z")

</div>

A better way to do this would be with a function, e.g.

```julia
debug() = false

function foo(value)
    if debug()
        println("value = ", value)
    end
end

```

then

```julia-repl
julia> code_llvm(foo, Tuple{Int})
; Function Signature: foo(Int64)
; @ REPL[26]:1 within `foo`
define void @julia_foo_18366(i64 signext %"value::Int64") #0 {
top:
  ret void
}

```

and if you change `debug()`, then any functions which depend on it will be automatically recompiled when they’re called again:

```julia-repl
julia> debug() = true;

julia> code_llvm(foo, Tuple{Int})
; Function Signature: foo(Int64)
; @ REPL[26]:1 within `foo`
define void @julia_foo_18399(i64 signext %"value::Int64") #0 {
top:
; @ REPL[26]:3 within `foo`
  call void @j_println_18402(ptr nonnull @"jl_global#18403.jit", i64 signext %"value::Int64")
  ret void
}

```

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [December 6, 2024, 3:11pm UTC](https://discourse.julialang.org/t/is-there-a-feature-like-cs-define-in-order-to-compile-or-not-e-g-println/123542/3 "2024-12-06T15:11:38Z")

</div>

If you are interested in debug logs (i.e., it’s not just a toy example that you used as a substitute for another, more complex problem), then there is a `@debug` macro that can be controlled by an environment variable: [Logging · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Logging/#Environment-variables)

Copy-pasting an example from the referenced documentation:

```julia-repl
julia> foo() = @debug "foo"
foo (generic function with 1 method)

julia> foo()

julia> ENV["JULIA_DEBUG"] = Main
Main

julia> foo()
┌ Debug: foo
└ @ Main REPL[1]:1

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 6, 2024, 3:38pm UTC](https://discourse.julialang.org/t/is-there-a-feature-like-cs-define-in-order-to-compile-or-not-e-g-println/123542/4 "2024-12-06T15:38:01Z")

</div>

The closest analogue of C’s `#ifdef` in Julia is probably [`@static if`](https://docs.julialang.org/en/v1/base/base/#Base.@static). But you rarely need this — usually relying on the compiler for constant propagation suffices, and there are other ways to enable debugging logs as noted above.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 6, 2024, 5:07pm UTC](https://discourse.julialang.org/t/is-there-a-feature-like-cs-define-in-order-to-compile-or-not-e-g-println/123542/5 "2024-12-06T17:07:47Z")

</div>

> [@barucden](#):
>
> If you are interested in debug logs (i.e., it’s not just a toy example that you used as a substitute for another, more complex problem), then there is a `@debug` macro that can be controlled by an environment variable: [Logging · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Logging/#Environment-variables)
> 
> Copy-pasting an example from the referenced documentation:
> 
> ```julia
> julia> foo() = @debug "foo"
> foo (generic function with 1 method)
> 
> julia> foo()
> 
> julia> ENV["JULIA_DEBUG"] = Main
> Main
> 
> julia> foo()
> ┌ Debug: foo
> └ @ Main REPL[1]:1
> 
> ```

Note that this doesn’t do what the OP wanted though. The `@debug` code is still in the compiled function whether or now you have `ENV["JULIA_DEBUG"] = Main`, it’s just not displaying.
