2
char webpage[] PROGMEM = R"=====( 
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.6/svg.min.js"></script>
  </head>
  <body>
  </body>
</html>
)=====";

I found a problem when using the raw string literal in Arduino IDE. My code cannot compile if i have http:// link inside the string.

I found that it should be related to how Arduino IDE thought the string after // are comments and during compile time those string are ignored and my codes break.

It looks like the arduino ide's compiler removes all //comment in the code first, before it try to compile the raw string literal and then cause the issue.

As a test I try to debug it by changing my code to https:/-/ the program can be compile as now there is no // in the raw string literal

Is there any workaround?

0

3 Answers 3

3

Escape the two quote marks inside the string using a backslash (literal quotes).

Without the escape characters, the URL is "outside" of a string because the quote marks are string delimiters.

char webpage[] PROGMEM = R"=====( 
<html>
  <head>
    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.6/svg.min.js\"></script>
  </head>
  <body>
  </body>
</html>
)=====";
4
  • thanks. interesting. after escaping the " it can complie but the link doesn't work anymore because in the html the src link cannot point to the link location anymore, as it got the extra \ escape character before " Oct 26, 2018 at 6:28
  • Escaping characters are not left into the end result, you have some other issue, for example you can't link to external content when serving directly from an ESP8266/ESP32 because it doesn't have internet connection that could enable your device to download the resources.
    – Avamander
    Oct 26, 2018 at 11:14
  • This is just a simplified snippet of the code. My original program will serve a javascript page. I will post my full code later. Oct 27, 2018 at 4:16
  • For c11 raw string literal, the end result will contain the escape characters. Meaning \" in the raw string literal will be still display as \" Oct 27, 2018 at 7:49
3
char webpage[] PROGMEM = R"=="==( 
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.6/svg.min.js"></script>
  </head>
  <body>
  </body>
</html>
)=="==";

inspired by answer provided by @jsotola if i used the syntax like this R"=="==()=="==" the link will become inside the quote. and hence the ide's complier regards it as a string and won't think // is comment and now the code can complie and the link works.

0
0

The C & C++ compilers don't look for language syntax inside quoted strings (except for character-escapes such as '\n'). That is the point of quoting the string.

This program just compiled Ok for me:

const char webpage[] PROGMEM = R"=====( 
<html>
  <head>
    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.6/svg.min.js\"></script>
  </head>
  <body>
  </body>
</html>
)=====";

void setup() {
}

void loop() {
}

'//' within the string did not cause any problem. Your example did get an error message that the variable 'webpage' had to be const to put it in Flash memory so I added the 'const' keyword, but that is the only change I made.

If you do that, and your compiler is still complaining, there is some other issue than the '//'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.