Is WASM ignoring preprocessor command in some cases?

I have a few variables in my header file “module.h”, like this:

#ifndef __MODULE__H
#define __MODULE__H

bool WPSent;
enum INPUT_ID {
	INPUT_ZX,
};
...
#endif

However, the compiler keeps giving me “duplicate symbol” error on WPSent:

wasm-ld : error : duplicate symbol: WPSent

and is fine with the enum. I also tried

#pragma once

but that produces the same error. Is WASM ignoring preprocessor commands for certain variables or am I missing something? For now, I am resorting to using externs in my source files, but that is really annoying. Any thoughts on how to address this?

Thanks,
Mitch
TerraBuilder Team

Hi

This is not a wasm problem but a C++ one.

The guard you added (ifndef / define or #pragma once) works as expected. They will prevent your compiler to include this more than once in the same compilation unit. So, if you include this header in several cpp file (different compilation unit), this header will be included several times and your variable will be duplicated.

To solve that, you should :

  1. Keep guards in your code, this is a good habbit
  2. Add the “external” keyword before your global declaration
external bool WPSent;
  1. In one of your cpp, “module.cpp” for instance, declare and initialize your global variable
bool WPSent = false;
  1. You can now include your header in any cpp file you have.

Best Regards
Maxime / Asobo

Hi Maxime - thanks for your prompt reply!

I understand, but I have to admit I am still confused about your answer. These compiler guards work well and are a standard practice when including the same header file in several .cpp files (different compilation units)… that is their purpose. And, they work when compiled with the standard VS compiler. So, there must be something different between VS compiler and WASM compiler. The code I have is a module I used in P3D, compiled as DLL, and it compiled without any errors, so I was a bit concerned when it produced errors in WASM.

Sure - I know how to fix it (I’ll probably go with declaring all vars static) but I was wondering why is WASM “complaining” about this, and the standard VS is not?

Thanks,
Mitch
Terrabuilder Team

I think we covered it here… https://www.fsdeveloper.com/forum/threads/wasm-question-is-wasm-ignoring-preprocessor-command-in-some-cases.457845/.

I don’t want Maxime to waste their time, though of course not to preclude any further input. :slight_smile:

Cheers,
-Max(im)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.