I have a library with several macros, it compiles fine on AIX, but now i need to compile the same code and it seems the macros stopped to work.
I keep receiving the message:
error: pasting "::" and "EVENT_DATA" does not give a valid preprocessing token.
Is there a way to make the c++ preprocessor on linux acts like on aix.
I'm using g++ on linux and xlc_r on AIX.
Here is one of the macros.
#define E_TRA_INMOD(MName, Comp) \
static const ES_TracMg::ES_TracComps ES_TracComp = \
ES_TracMg::##Comp; \
static char* ES_Mod_Namp = MName; \
static unsigned long ES_SerMas = \
ES_TracMg::m_MServ[ES_TracMg##Comp];
I call it like E_TRA_INMOD("Error", EVENT_DATA);
The error is:
error: pasting "::" and "EVENT_DATA" does not give a valid preprocessing token.
I think you don't want to use ## here:
#define E_TRA_INMOD(MName, Comp) \
static const ES_TracMg::ES_TracComps ES_TracComp = \
ES_TracMg::##Comp; \
It should be
#define E_TRA_INMOD(MName, Comp) \
static const ES_TracMg::ES_TracComps ES_TracComp = \
ES_TracMg::Comp; \
You don't have two tokens to glue together into a single token, you just have whatever Comp expands to.
What are you trying to do in the macro? It looks like the first token paste is redundant:
#define E_TRA_INMOD(MName,Comp) \
static const ES_TracMg::ES_TracComps ES_TracComp = \
ES_TracMg::Comp; \
static char* ES_Mod_Namp = MName; \
static unsigned long ES_SerMas = \
ES_TracMg::m_MServ[ES_TracMg##Comp];
Related
Trying to simplify writing boiler plate, but I get Field cannot have type 'void' and / or Expected ')'
Newbie c++, I've seen a bunch questions like this, but still can't figure it out. The errors are still too cryptic to me to able to google them..
#define GAME_STAT(Stat) \
UPROPERTY(BlueprintReadOnly, Category = "Stats", ReplicatedUsing = OnRep_##Stat##) \
FGameplayAttributeData ##Stat##; \
GAME_STAT_ACCESS(UGameStats, ##Stat##); \
UFUNCTION() \
virtual void OnRep_##Stat##(const FGameplayAttributeData& Old##Stat##);
GAME_STAT("Health")
I want to generate the code with word "Health" instead of stand-in "Stat"
Thanks!
## is for pasting tokens together, but it looks like you think it is "reverse stringification".
It's also a binary operator, not an "around-ary" operator.
That is,
#define hello(x) Hello_##x
hello(World)
will produce
Hello_World
This should work (but is thoroughly untested):
#define GAME_STAT(Stat) \
UPROPERTY(BlueprintReadOnly, Category = "Stats", ReplicatedUsing = OnRep_##Stat) \
FGameplayAttributeData Stat; \
GAME_STAT_ACCESS(UGameStats, Stat); \
UFUNCTION() \
virtual void OnRep_##Stat(const FGameplayAttributeData& Old##Stat);
GAME_STAT(Health)
I am having trouble building standalone webassembly with the full control I want over memory and layout. I don't want to use emscripten because, as the following post says, it doesn't give me all of the compile-time options I want (e.g. stack size control, being able to choose to import memory in standalone mode, etc.) I've been folowing pages such as: How to generate standalone webassembly with emscripten
Also, emscripten is overkill.
What I've done so far:
I have a fully working llvm 9 toolchain downloaded via homebrew (I am on macos 10.14.)
I was following a mix of https://aransentin.github.io/cwasm/ and https://depth-first.com/articles/2019/10/16/compiling-c-to-webassembly-and-running-it-without-emscripten/
I used wasi to get the C standard library. Using linker flags like -Wl,-z,stack-size=$[1024 * 1024] I could control the stack size. Compilation was successful. Great!
However, I need to use C++ standard libraries to support some of my own and other third party libraries.
As far as I can tell, there doesn't seem to be any easy way to get libc++ and libc++abi.
I tried a "hack" in which I downloaded Emscripten and had it build its own libc++ and libc++abi files. Then I tried copying those files and headers into the right spot.
Then I got error messages referring to a missing threading API, which apparently were caused by not compiling with EMSCRIPTEN. So I defined the EMSCRIPTEN macro and that sort of worked. Then I thought that maybe I could remove the wasi dependency and use emscripten's version of libc to be consistent, but then there were conflicting / missing headers too.
In short, I think I got somewhat close to where I needed to be, but things just got terribly messy. I doubt I took the simplest non-emscripten approach.
Has anyone successfully created a build system for standalone webassembly that lets you use the c and c++ standard libraries?
EDIT:
This is the super hacky build script I have now (it's a heavily modified version of something I found online):
DEPS =
OBJ = library.o
STDLIBC_OBJ = $(patsubst %.cpp,%.o,$(wildcard stdlibc/*.cpp))
OUTPUT = library.wasm
DIR := ${CURDIR}
COMPILE_FLAGS = -Wall \
--target=wasm32-unknown-wasi \
-Os \
-D __EMSCRIPTEN__ \
-D _LIBCPP_HAS_NO_THREADS \
-flto \
--sysroot ./ \
-std=c++17 \
-ffunction-sections \
-fdata-sections \
-I./libcxx/ \
-I./libcxx/support/xlocale \
-I./libc/include \
-DPRINTF_DISABLE_SUPPORT_FLOAT=1 \
-DPRINTF_DISABLE_SUPPORT_LONG_LONG=1 \
-DPRINTF_DISABLE_SUPPORT_PTRDIFF_T=1
$(OUTPUT): $(OBJ) $(NANOLIBC_OBJ) Makefile
wasm-ld \
-o $(OUTPUT) \
--no-entry \
--export-all \
--initial-memory=131072 \
--stack-size=$[1024 * 1024] \
-error-limit=0 \
--lto-O3 \
-O3 \
-lc -lc++ -lc++abi \
--gc-sections \
-allow-undefined-file ./stdlibc/wasm.syms \
$(OBJ) \
$(LIBCXX_OBJ) \
$(STDLIBC_OBJ)
%.o: %.cpp $(DEPS) Makefile
clang++ \
-c \
$(COMPILE_FLAGS) \
-fno-exceptions \
-o $# \
$<
library.wat: $(OUTPUT) Makefile
~/build/wabt/wasm2wat -o library.wat $(OUTPUT)
wat: library.wat
clean:
rm -f $(OBJ) $(STDLIBC_OBJ) $(OUTPUT) library.wat
I dropped-in libc, libc++, and libc++abi from emscripten (but honestly this is a terrible installation process.)
I've been incrementally trying to fill in gaps that I guess emscripten would've normally done, but now I'm stuck again:
./libcxx/type_traits:4837:57: error: use of undeclared identifier 'byte'
constexpr typename enable_if<is_integral_v<_Integer>, byte>::type &
^
./libcxx/type_traits:4837:64: error: definition or redeclaration of 'type'
cannot name the global scope
constexpr typename enable_if<is_integral_v<_Integer>, byte>::type &
I am no longer sure if this will even work since the system might accidentally compile something platform-specific in. Really what I'd like is a shim that would just let me use the standard containers mostly.
This has become kind of unmanageable. What might I do next?
EDIT 2: Right so that's missing C++17 type trait content, and when I go to C++14 (I still want C++17) I end up with more missing things.
Definitely stuck.
EDIT 3:
I sort of started over. The libraries are linking, and I'm able to use the standard, but I'm seeing errors like the following if I try to use e.g. an std::chrono's methods (I can instantiate the object):
wasm-ld: error: /var/folders/9k/zvv02vlj007cc0pm73769y500000gn/T/library-4ff1b5.o: undefined symbol: std::__1::chrono::system_clock::now()
I'm currently using the static library abi from emscripten and the static library C++ standard library from my homebrew installation of llvm (I tried the emscripten one but that didn't work either).
I'm not really sure if this is related to name mangling. I'm currently exporting all symbols from webasm so malloc and co. get exported as well.
Here is my build script:
clang++ \
--target=wasm32-unknown-wasi \
--std=c++11 \
-stdlib=libc++ \
-O3 \
-flto \
-fno-exceptions \
-D WASM_BUILD \
-D _LIBCPP_HAS_NO_THREADS \
--sysroot /usr/local/opt/wasi-libc \
-I/usr/local/opt/wasi-libc/include \
-I/usr/local/opt/glm/include \
-I./libcxx/ \
-L./ \
-lc++ \
-lc++abi \
-nostartfiles \
-Wl,-allow-undefined-file wasm.syms \
-Wl,--import-memory \
-Wl,--no-entry \
-Wl,--export-all \
-Wl,--lto-O3 \
-Wl,-lc++, \
-Wl,-lc++abi, \
-Wl,-z,stack-size=$[1024 * 1024] \
-o library.wasm \
library.cpp
My code:
#include "common_header.h"
#include <glm/glm.hpp>
#include <unordered_map>
#include <vector>
#include <string>
#include <chrono>
template <typename T>
struct BLA {
T x;
};
template <typename T>
BLA<T> make_BLA() {
BLA<T> bla;
std::unordered_map<T, T> map;
std::vector<T> bla2;
std::string str = "WEE";
//str = str.substr(0, 2);
return bla;
}
#ifdef __cplusplus
extern "C" {
#endif
char* malloc_copy(char* input)
{
usize len = strlen(input) + 1;
char* result = (char*)malloc(len);
if (result == NULL) {
return NULL;
}
strncpy(result, input, len);
return result;
}
void malloc_free(char* input)
{
free(input);
}
float32 print_num(float val);
float32 my_sin(float32 val)
{
float32 result = sinf(val);
float32 result_times_2 = print_num(result);
print_num(result_times_2);
return result;
}
long fibonacci(unsigned n) {
if (n < 2) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
void set_char(char* input)
{
input[0] = '\'';
uint8 fibonacci_series[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
for (uint8 number : fibonacci_series) {
input[0] = number;
}
auto WEE = make_BLA<int>();
WEE.x = 18;
glm::vec4 v(100.0f, 200.0f, 300.0f, 1.0f);
glm::vec4 v_out = glm::mat4(1.0f) * v;
input[0] = 5 + static_cast<int>(v_out.x) * input[1];
auto start = std::chrono::system_clock::now();
long out = fibonacci(42);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
auto elapsed = elapsed_seconds.count();
}
#ifdef __cplusplus
}
#endif
When I tried exporting dynamically with the "visible" attribute on only the functions that had no C++, the project compiled, but the wasm module failed to load in JavaScript, so I think the problem was still there.
This is as far as I've gotten. Might the issue be related to the fact that I'm using a different compiler from the one used to create the static libraries? (I'm using homebrew clang 9). Hopefully not. I'd be kind of stuck then because I couldn't find another way to get the libraries. Manual llvm compilation seemed to fail.
The excellent wasi-sdk pulls upstream llvm-project (which provides clang++) and wasi-libc as git submodules and compiles them using suitable flags (most notably disabling pthreads which is not yet supported in wasi-libc).
You can then compile your own C++ source using the following minimal set of options:
/path/to/wasi-sdk/build/install/opt/wasi-sdk/bin/clang++ \
-nostartfiles \
-fno-exceptions \
-Wl,--no-entry \
-Wl,--strip-all \
-Wl,--export-dynamic \
-Wl,--import-memory \
-fvisibility=hidden \
--sysroot /path/to/wasi-sdk/build/install/opt/wasi-sdk/share/wasi-sysroot \
-o out.wasm \
source.cpp
If you want to import functions from the runtime, I would suggest adding an additional line:
-Wl,--allow-undefined-file=wasm-import.syms \
You then can put function names separated by newlines into wasm-import.syms so that the linker won't complain about undefined functions.
Note that all this is completely independent of Emscripten.
I have written a macros but when i use the code i get expected identifier error.
Below is my macros
#define ITK(arguments) \
{ \
int iFail=0; \
iFail = arguments; \
if(iFail != ITK_ok) \
{ \
char* s; \
TC_write_syslog("Following Method Retruns error "#arguments "\n");\
TC_write_syslog("Error is in the line [%d] of the file ["__FILE__"]\n",[__LINE__]);\
EMH_ask_error_text(iFail,&s);\
TC_write_syslog("And the error is [%s]",s);\
if(s!=0) MEM_FREE(s);\
} \
}
As Davis Herring said the reason is your [__LINE__]
When you are lost and you do not see where is the error on that kind of case look at the code after the pre processing, for instance using g++ do g++ -E < other flags except -c > -o temp.cc and if you still don't see in temp.cc add line breaks in the interesting portion of code in temp.cc then compile temp.cc to look at compiler errors/warnings
Looks like a simple typo to me. You need to remove [ and ] here:
TC_write_syslog("Error is in the line [%d] of the file ["__FILE__"]\n",[__LINE__]);
// ^ ^
Note that instead of using printf's %d to insert __LINE__ into a string at runtime, you can convert __LINE__ to a string literal at compile-time:
#define STR(x) STR_(x)
#define STR_(x) #x
TC_write_syslog("Error is in the line " STR(__LINE__) " of the file ["__FILE__"]\n");
I have started working with Neural Networks, so I got a FANN library (http://leenissen.dk/fann/wp/). I am having problems to compile it, specifically file
/fann-master/src/fann_io.c
where I am receiving a error on line 346:
fann_io.c:346:29: error: unable to find string literal operator ‘operator""type’ with ‘const char [20]’, ‘long unsigned int’ arguments
if(fscanf(conf, name"="type"\n", val) != 1) \
And I have looked it up, but I still have no idea how to fix it. Here is the function.
#define fann_scanf(type, name, val) \
{ \
if(fscanf(conf, name"="type"\n", val) != 1) \
{ \
fann_error(NULL, FANN_E_CANT_READ_CONFIG, name, configuration_file); \
fann_destroy(ann); \
return NULL; \
} \
}
The solution which helped me was to use compiler option
-std=c++03
So in the end I could compile the whole project by typing in
g++ main.cpp -std=c++03
I have the following c++ code:
#define MATH_FASTCALL(name, fname) \
static int math_ ## name ## _precall (lua_State *L, StkId func, int nresults) { \
StkId arg1 = func + 1; \
llvm_arg_tonumber(L, arg1, 1); \
setnvalue(func, fname(nvalue(arg1))); \
L->ci--; \
L->top = func + 1; \
L->base = L->ci->base; \
return PCRC; \
fallback: \
return luaD_precall_c(L, func, nresults); \
}
MATH_FASTCALL(abs, fabs)
When I try to compile it in Visual Studio 14 2015 it gives an error at the line with MATH_FASTCALL:
expression must be a pointer to a complete object type
What could be the problem with this code?
The error message makes it sound like the macro is dereferencing a pointer to an incomplete type. Make sure that lua_State and the type of lua_State::ci are defined (not just declared) before invoking the macro.