ATTENTION! Please read and follow:
- if this is a _question_ about how to build / test / query / deploy using Bazel, or a _discussion starter_, send it to [email protected]
- if this is a _bug_ or _feature request_, fill the form below as best as you can.
Cannot #define a variable as a string literal.
Replace this line with your answer.
BUILD
:
cc_binary(
name = "hello-world",
srcs = ["main.cc"],
defines = ["FOO=\"BAR\""],
# defines = ['FOO="BAR"'],
# defines = ['FOO=\"BAR\"'],
# defines = [r'FOO="BAR"'],
)
main.cc
:
#include <iostream>
// #define FOO "BAR"
int main() {
#if defined(FOO)
std::cout << "Foo = " << FOO << std::endl;
#else
std::cout << "Hello World!" << std::endl;
#endif
}
This example should compile but any definition above causes the double quotes to disappear, which makes the above code look like BAR
is an undeclared variable.
MacOS 10.15
bazel info release
?release 2.0.0-homebrew
bazel info release
returns "development version" or "(@non-git)", tell us how you built Bazel.NA (homebrew)
git remote get-url origin ; git rev-parse master ; git rev-parse HEAD
?Replace this line with your answer.
https://www.reddit.com/r/bazel/comments/dslohm/cc_library_local_defines_possible_to_escape_for/
Replace these lines with your answer.
If the files are large, upload as attachment or provide link.
The following should work in your build rule:
defines = ["FOO=\\\"BAR\\\""]
defines = ['FOO=\\"BAR\\"']
You're subject to two rounds of tokenization; parsing of the BUILD file and then Bourne shell's command line parser. You need the literal FOO=\"BAR\"
to reach the command line for the compiler, hence the double escaping.
Doh! Such a simple fix. Thank you!
I would argue that backslash escaping is a hack around rather than a full
solution. We should not have to assume Bourne shell parsing. That is *nix
centric.
A complete solution would be to let the user write
defines=["var=value that may have spaces"]
The CC toolchain would do whatever quoting is needed to get the correct arg
passed to whatever compiler we happen to be using
On Thu, Mar 5, 2020, 6:59 PM Harsh Mohan notifications@github.com wrote:
Doh! Such a simple fix. Thank you!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/bazelbuild/bazel/issues/10859?email_source=notifications&email_token=AAXHHHCHJI5PUQ7DUJXLCADRGA4EVA5CNFSM4K3CAXZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEN7KNPY#issuecomment-595502783,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAXHHHFU4NXAKI6GUHR3ORLRGA4EVANCNFSM4K3CAXZA
.