I have nim C++ project (nim cpp --backend cpp to compile), having .c as an dependency.
If I passed --passC:"--std=c++17", clang causes error on compiling .c files.
Work-around is to rename .c into .cpp and wrap extern "C" { ... } whole the file, but I don't like use this...
Any way to pass compiler flag only to c++ compiler, not to c compiler?
separating flags into passC (for C files) and passCpp (for C++ files) is the standard, supported way to handle this I think,
You should try passCpp instead.
Use the two-argument version of compile: https://nim-lang.org/docs/manual.html#implementation-specific-pragmas-compile-pragma
{.compile: ("myfile.cpp", "--std=c++17").}
{.passC.} adds the flags globally which is typically not what you want for compile.
so this would be messy
Typically, we solve this with a constant (it can also be done with a macro, but that's probably overkill):
const cppFlags = "--std=c++17"
{.compile: ("myfile.cpp", cppFlags).}
This is also useful for integrating third-party tools like pkg-config:
const SomePackageFlags = gorge "pkg-config --cflags SomePackage"
{.compile: ("myfile.cpp", SomePackageFlags).}I mean you should use like based on compiler try this once:
switch("gcc.cpp.options.always", "-std=c++17")
switch("clang.cpp.options.always", "-std=c++17")
I confirmed cpp.options.always is working on UNIX enviroments.
--passC:"-std=c++17" causes problems on only UNIX environments, not windows (I don't know why), so I made conditional build flags to solve.
when defined(windows):
switch("passC", "/std:c++17")
else:
# switch("passC", "-std=c++17")
switch("cpp.options.always", "-std=c++17")
Now I could {.compile: .c files mixed on .cpp files. Thank you very much.