How can I have code in a project that won't compile for Arduino?











up vote
5
down vote

favorite












I want to test the algorithms in my code without needing to send it to an actual Arduino board. I figured I could lay out my project like:



project/
core.cpp # core algorithms
core.h # header for core.cpp
project.ino # Arduino setup() loop() calling into core
pc-main.cpp # main(argv) calling into core


However, when I do this, the Arduino IDE wants to compile pc-main.cpp, which won't work since it uses stdio and all that. What can I do instead?



Is there a way to hide a file from the build, other than changing the extension which would inconvenience other editors and compilers? Is there a different standard approach to this kind of problem?





What I've thought of:




  • I could put pc-main.cpp in another directory and have the build for it refer to the files in the Arduino project directory, but that seems awkward and I'd like a more elegant solution.


  • I could symlink the core.* files into the Arduino project directory from elsewhere, but that would also be inelegant, and inconvenience Windows users should one want to compile the project.


  • I could put an #ifdef around the contents of pc-main.cpp, but I haven't found a suitable #define to check for, other than board-specific ones.



I don't want a solution which requires extra steps for each build; the whole point of trying to do this is fast and easy development.



I would also prefer one which does not bring in a whole additional build system, testing framework, or IDE; for example, I found arduino_ci and PlatformIO while researching this question. I'll switch to looking at such options if there isn't a good solution to the problem as I've stated it here.





[My question is arguably the same as Project structure to build for PC and Arduino at a high level, but the question and answer there are broad and about organizing modules and not how to make the build actually work.]










share|improve this question







New contributor




Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    5
    down vote

    favorite












    I want to test the algorithms in my code without needing to send it to an actual Arduino board. I figured I could lay out my project like:



    project/
    core.cpp # core algorithms
    core.h # header for core.cpp
    project.ino # Arduino setup() loop() calling into core
    pc-main.cpp # main(argv) calling into core


    However, when I do this, the Arduino IDE wants to compile pc-main.cpp, which won't work since it uses stdio and all that. What can I do instead?



    Is there a way to hide a file from the build, other than changing the extension which would inconvenience other editors and compilers? Is there a different standard approach to this kind of problem?





    What I've thought of:




    • I could put pc-main.cpp in another directory and have the build for it refer to the files in the Arduino project directory, but that seems awkward and I'd like a more elegant solution.


    • I could symlink the core.* files into the Arduino project directory from elsewhere, but that would also be inelegant, and inconvenience Windows users should one want to compile the project.


    • I could put an #ifdef around the contents of pc-main.cpp, but I haven't found a suitable #define to check for, other than board-specific ones.



    I don't want a solution which requires extra steps for each build; the whole point of trying to do this is fast and easy development.



    I would also prefer one which does not bring in a whole additional build system, testing framework, or IDE; for example, I found arduino_ci and PlatformIO while researching this question. I'll switch to looking at such options if there isn't a good solution to the problem as I've stated it here.





    [My question is arguably the same as Project structure to build for PC and Arduino at a high level, but the question and answer there are broad and about organizing modules and not how to make the build actually work.]










    share|improve this question







    New contributor




    Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I want to test the algorithms in my code without needing to send it to an actual Arduino board. I figured I could lay out my project like:



      project/
      core.cpp # core algorithms
      core.h # header for core.cpp
      project.ino # Arduino setup() loop() calling into core
      pc-main.cpp # main(argv) calling into core


      However, when I do this, the Arduino IDE wants to compile pc-main.cpp, which won't work since it uses stdio and all that. What can I do instead?



      Is there a way to hide a file from the build, other than changing the extension which would inconvenience other editors and compilers? Is there a different standard approach to this kind of problem?





      What I've thought of:




      • I could put pc-main.cpp in another directory and have the build for it refer to the files in the Arduino project directory, but that seems awkward and I'd like a more elegant solution.


      • I could symlink the core.* files into the Arduino project directory from elsewhere, but that would also be inelegant, and inconvenience Windows users should one want to compile the project.


      • I could put an #ifdef around the contents of pc-main.cpp, but I haven't found a suitable #define to check for, other than board-specific ones.



      I don't want a solution which requires extra steps for each build; the whole point of trying to do this is fast and easy development.



      I would also prefer one which does not bring in a whole additional build system, testing framework, or IDE; for example, I found arduino_ci and PlatformIO while researching this question. I'll switch to looking at such options if there isn't a good solution to the problem as I've stated it here.





      [My question is arguably the same as Project structure to build for PC and Arduino at a high level, but the question and answer there are broad and about organizing modules and not how to make the build actually work.]










      share|improve this question







      New contributor




      Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I want to test the algorithms in my code without needing to send it to an actual Arduino board. I figured I could lay out my project like:



      project/
      core.cpp # core algorithms
      core.h # header for core.cpp
      project.ino # Arduino setup() loop() calling into core
      pc-main.cpp # main(argv) calling into core


      However, when I do this, the Arduino IDE wants to compile pc-main.cpp, which won't work since it uses stdio and all that. What can I do instead?



      Is there a way to hide a file from the build, other than changing the extension which would inconvenience other editors and compilers? Is there a different standard approach to this kind of problem?





      What I've thought of:




      • I could put pc-main.cpp in another directory and have the build for it refer to the files in the Arduino project directory, but that seems awkward and I'd like a more elegant solution.


      • I could symlink the core.* files into the Arduino project directory from elsewhere, but that would also be inelegant, and inconvenience Windows users should one want to compile the project.


      • I could put an #ifdef around the contents of pc-main.cpp, but I haven't found a suitable #define to check for, other than board-specific ones.



      I don't want a solution which requires extra steps for each build; the whole point of trying to do this is fast and easy development.



      I would also prefer one which does not bring in a whole additional build system, testing framework, or IDE; for example, I found arduino_ci and PlatformIO while researching this question. I'll switch to looking at such options if there isn't a good solution to the problem as I've stated it here.





      [My question is arguably the same as Project structure to build for PC and Arduino at a high level, but the question and answer there are broad and about organizing modules and not how to make the build actually work.]







      arduino-ide compile build






      share|improve this question







      New contributor




      Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 14 at 16:32









      Kevin Reid

      1284




      1284




      New contributor




      Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Kevin Reid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          9
          down vote



          accepted










          I would use conditional compilation, like this:



          #ifndef(ARDUINO)
          // Non-Arduino code.
          #endif





          share|improve this answer





















          • Thanks. Is there official documentation that specifies this #define exists?
            – Kevin Reid
            Nov 14 at 17:10










          • You can add the #define to your code and manually change it when you change environments. You can look up Arduino compiler predefined symbols. Or you can tweak the IDE's compiler command to include "-DSYMBOL_OF_MY_CHOICE" which would define your symbol during an IDE compile and presumably be undefined in your desktop environment.
            – JRobert
            Nov 14 at 17:46










          • To be clear, I found that ARDUINO is already defined and I didn't need to configure anything, so Edgar's code works exactly as written. I'm asking if there's documentation for it, as I think the answer would be even better with a link.
            – Kevin Reid
            Nov 14 at 18:01










          • @KevinReid: I didn't find it in a doc (although it's mentioned here), but in a Makefile intended to emulate the Arduino IDE.
            – Edgar Bonet
            Nov 14 at 19:58











          Your Answer






          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("schematics", function () {
          StackExchange.schematics.init();
          });
          }, "cicuitlab");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "540"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Kevin Reid is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f57822%2fhow-can-i-have-code-in-a-project-that-wont-compile-for-arduino%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          9
          down vote



          accepted










          I would use conditional compilation, like this:



          #ifndef(ARDUINO)
          // Non-Arduino code.
          #endif





          share|improve this answer





















          • Thanks. Is there official documentation that specifies this #define exists?
            – Kevin Reid
            Nov 14 at 17:10










          • You can add the #define to your code and manually change it when you change environments. You can look up Arduino compiler predefined symbols. Or you can tweak the IDE's compiler command to include "-DSYMBOL_OF_MY_CHOICE" which would define your symbol during an IDE compile and presumably be undefined in your desktop environment.
            – JRobert
            Nov 14 at 17:46










          • To be clear, I found that ARDUINO is already defined and I didn't need to configure anything, so Edgar's code works exactly as written. I'm asking if there's documentation for it, as I think the answer would be even better with a link.
            – Kevin Reid
            Nov 14 at 18:01










          • @KevinReid: I didn't find it in a doc (although it's mentioned here), but in a Makefile intended to emulate the Arduino IDE.
            – Edgar Bonet
            Nov 14 at 19:58















          up vote
          9
          down vote



          accepted










          I would use conditional compilation, like this:



          #ifndef(ARDUINO)
          // Non-Arduino code.
          #endif





          share|improve this answer





















          • Thanks. Is there official documentation that specifies this #define exists?
            – Kevin Reid
            Nov 14 at 17:10










          • You can add the #define to your code and manually change it when you change environments. You can look up Arduino compiler predefined symbols. Or you can tweak the IDE's compiler command to include "-DSYMBOL_OF_MY_CHOICE" which would define your symbol during an IDE compile and presumably be undefined in your desktop environment.
            – JRobert
            Nov 14 at 17:46










          • To be clear, I found that ARDUINO is already defined and I didn't need to configure anything, so Edgar's code works exactly as written. I'm asking if there's documentation for it, as I think the answer would be even better with a link.
            – Kevin Reid
            Nov 14 at 18:01










          • @KevinReid: I didn't find it in a doc (although it's mentioned here), but in a Makefile intended to emulate the Arduino IDE.
            – Edgar Bonet
            Nov 14 at 19:58













          up vote
          9
          down vote



          accepted







          up vote
          9
          down vote



          accepted






          I would use conditional compilation, like this:



          #ifndef(ARDUINO)
          // Non-Arduino code.
          #endif





          share|improve this answer












          I would use conditional compilation, like this:



          #ifndef(ARDUINO)
          // Non-Arduino code.
          #endif






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 at 16:36









          Edgar Bonet

          23.1k22344




          23.1k22344












          • Thanks. Is there official documentation that specifies this #define exists?
            – Kevin Reid
            Nov 14 at 17:10










          • You can add the #define to your code and manually change it when you change environments. You can look up Arduino compiler predefined symbols. Or you can tweak the IDE's compiler command to include "-DSYMBOL_OF_MY_CHOICE" which would define your symbol during an IDE compile and presumably be undefined in your desktop environment.
            – JRobert
            Nov 14 at 17:46










          • To be clear, I found that ARDUINO is already defined and I didn't need to configure anything, so Edgar's code works exactly as written. I'm asking if there's documentation for it, as I think the answer would be even better with a link.
            – Kevin Reid
            Nov 14 at 18:01










          • @KevinReid: I didn't find it in a doc (although it's mentioned here), but in a Makefile intended to emulate the Arduino IDE.
            – Edgar Bonet
            Nov 14 at 19:58


















          • Thanks. Is there official documentation that specifies this #define exists?
            – Kevin Reid
            Nov 14 at 17:10










          • You can add the #define to your code and manually change it when you change environments. You can look up Arduino compiler predefined symbols. Or you can tweak the IDE's compiler command to include "-DSYMBOL_OF_MY_CHOICE" which would define your symbol during an IDE compile and presumably be undefined in your desktop environment.
            – JRobert
            Nov 14 at 17:46










          • To be clear, I found that ARDUINO is already defined and I didn't need to configure anything, so Edgar's code works exactly as written. I'm asking if there's documentation for it, as I think the answer would be even better with a link.
            – Kevin Reid
            Nov 14 at 18:01










          • @KevinReid: I didn't find it in a doc (although it's mentioned here), but in a Makefile intended to emulate the Arduino IDE.
            – Edgar Bonet
            Nov 14 at 19:58
















          Thanks. Is there official documentation that specifies this #define exists?
          – Kevin Reid
          Nov 14 at 17:10




          Thanks. Is there official documentation that specifies this #define exists?
          – Kevin Reid
          Nov 14 at 17:10












          You can add the #define to your code and manually change it when you change environments. You can look up Arduino compiler predefined symbols. Or you can tweak the IDE's compiler command to include "-DSYMBOL_OF_MY_CHOICE" which would define your symbol during an IDE compile and presumably be undefined in your desktop environment.
          – JRobert
          Nov 14 at 17:46




          You can add the #define to your code and manually change it when you change environments. You can look up Arduino compiler predefined symbols. Or you can tweak the IDE's compiler command to include "-DSYMBOL_OF_MY_CHOICE" which would define your symbol during an IDE compile and presumably be undefined in your desktop environment.
          – JRobert
          Nov 14 at 17:46












          To be clear, I found that ARDUINO is already defined and I didn't need to configure anything, so Edgar's code works exactly as written. I'm asking if there's documentation for it, as I think the answer would be even better with a link.
          – Kevin Reid
          Nov 14 at 18:01




          To be clear, I found that ARDUINO is already defined and I didn't need to configure anything, so Edgar's code works exactly as written. I'm asking if there's documentation for it, as I think the answer would be even better with a link.
          – Kevin Reid
          Nov 14 at 18:01












          @KevinReid: I didn't find it in a doc (although it's mentioned here), but in a Makefile intended to emulate the Arduino IDE.
          – Edgar Bonet
          Nov 14 at 19:58




          @KevinReid: I didn't find it in a doc (although it's mentioned here), but in a Makefile intended to emulate the Arduino IDE.
          – Edgar Bonet
          Nov 14 at 19:58










          Kevin Reid is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Kevin Reid is a new contributor. Be nice, and check out our Code of Conduct.













          Kevin Reid is a new contributor. Be nice, and check out our Code of Conduct.












          Kevin Reid is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f57822%2fhow-can-i-have-code-in-a-project-that-wont-compile-for-arduino%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?