Error calling C++ function from R - library loads, but function is not on the table









up vote
2
down vote

favorite












Here is the C code:



// a.cpp
void double_me(int* x)
// takes a numeric input and doubles it
*x = *x + *x;



I compile the code with



>R CMD SHLIB a.cpp


After that i run R and type following commands:



 dinfo <- dyn.load("a.so")
.C("double_me",x=2)


This end with error: "double_me" is not on the list.



Now the question:
dyn.load works fine, dinfo contains:




DLL name: a Filename: /Users/myusername/a.so Dynamic lookup: TRUE




But the function is not on the table:




is.loaded("double_me")
[1] FALSE




How could it happen? This happens on macOS.










share|improve this question



























    up vote
    2
    down vote

    favorite












    Here is the C code:



    // a.cpp
    void double_me(int* x)
    // takes a numeric input and doubles it
    *x = *x + *x;



    I compile the code with



    >R CMD SHLIB a.cpp


    After that i run R and type following commands:



     dinfo <- dyn.load("a.so")
    .C("double_me",x=2)


    This end with error: "double_me" is not on the list.



    Now the question:
    dyn.load works fine, dinfo contains:




    DLL name: a Filename: /Users/myusername/a.so Dynamic lookup: TRUE




    But the function is not on the table:




    is.loaded("double_me")
    [1] FALSE




    How could it happen? This happens on macOS.










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Here is the C code:



      // a.cpp
      void double_me(int* x)
      // takes a numeric input and doubles it
      *x = *x + *x;



      I compile the code with



      >R CMD SHLIB a.cpp


      After that i run R and type following commands:



       dinfo <- dyn.load("a.so")
      .C("double_me",x=2)


      This end with error: "double_me" is not on the list.



      Now the question:
      dyn.load works fine, dinfo contains:




      DLL name: a Filename: /Users/myusername/a.so Dynamic lookup: TRUE




      But the function is not on the table:




      is.loaded("double_me")
      [1] FALSE




      How could it happen? This happens on macOS.










      share|improve this question















      Here is the C code:



      // a.cpp
      void double_me(int* x)
      // takes a numeric input and doubles it
      *x = *x + *x;



      I compile the code with



      >R CMD SHLIB a.cpp


      After that i run R and type following commands:



       dinfo <- dyn.load("a.so")
      .C("double_me",x=2)


      This end with error: "double_me" is not on the list.



      Now the question:
      dyn.load works fine, dinfo contains:




      DLL name: a Filename: /Users/myusername/a.so Dynamic lookup: TRUE




      But the function is not on the table:




      is.loaded("double_me")
      [1] FALSE




      How could it happen? This happens on macOS.







      c++ r dll






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 15:12

























      asked Nov 11 at 15:03









      Alexander Sobolev

      661311




      661311






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          This is because you are using a.cpp; C++ function names are "mangled" by the compiler. You can use your same code with the filename a.c, compiling it just as you did, and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Or, alternatively, you can add this line to the top of a.cpp:



          extern "C" void double_me(int* x);


          and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Update: Why was the result above 2?



          If you do not coerce the argument to the proper type, a copy may be made, such that your original value is not altered; if we coerce the value to be an integer as we should when using .C(), we get the expected result:



          > dyn.load("a.so")
          > .C("double_me", x = as.integer(2))
          $x
          [1] 4





          share|improve this answer






















          • This works! Thank you very much!
            – Alexander Sobolev
            Nov 11 at 15:14











          • $x --> [1] 4 (typo?)
            – Alexander Sobolev
            Nov 13 at 20:12






          • 1




            @AlexanderSobolev Not quite; I've add some info to explain
            – duckmayr
            Nov 13 at 20:28










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250010%2ferror-calling-c-function-from-r-library-loads-but-function-is-not-on-the-ta%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
          3
          down vote



          accepted










          This is because you are using a.cpp; C++ function names are "mangled" by the compiler. You can use your same code with the filename a.c, compiling it just as you did, and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Or, alternatively, you can add this line to the top of a.cpp:



          extern "C" void double_me(int* x);


          and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Update: Why was the result above 2?



          If you do not coerce the argument to the proper type, a copy may be made, such that your original value is not altered; if we coerce the value to be an integer as we should when using .C(), we get the expected result:



          > dyn.load("a.so")
          > .C("double_me", x = as.integer(2))
          $x
          [1] 4





          share|improve this answer






















          • This works! Thank you very much!
            – Alexander Sobolev
            Nov 11 at 15:14











          • $x --> [1] 4 (typo?)
            – Alexander Sobolev
            Nov 13 at 20:12






          • 1




            @AlexanderSobolev Not quite; I've add some info to explain
            – duckmayr
            Nov 13 at 20:28














          up vote
          3
          down vote



          accepted










          This is because you are using a.cpp; C++ function names are "mangled" by the compiler. You can use your same code with the filename a.c, compiling it just as you did, and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Or, alternatively, you can add this line to the top of a.cpp:



          extern "C" void double_me(int* x);


          and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Update: Why was the result above 2?



          If you do not coerce the argument to the proper type, a copy may be made, such that your original value is not altered; if we coerce the value to be an integer as we should when using .C(), we get the expected result:



          > dyn.load("a.so")
          > .C("double_me", x = as.integer(2))
          $x
          [1] 4





          share|improve this answer






















          • This works! Thank you very much!
            – Alexander Sobolev
            Nov 11 at 15:14











          • $x --> [1] 4 (typo?)
            – Alexander Sobolev
            Nov 13 at 20:12






          • 1




            @AlexanderSobolev Not quite; I've add some info to explain
            – duckmayr
            Nov 13 at 20:28












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          This is because you are using a.cpp; C++ function names are "mangled" by the compiler. You can use your same code with the filename a.c, compiling it just as you did, and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Or, alternatively, you can add this line to the top of a.cpp:



          extern "C" void double_me(int* x);


          and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Update: Why was the result above 2?



          If you do not coerce the argument to the proper type, a copy may be made, such that your original value is not altered; if we coerce the value to be an integer as we should when using .C(), we get the expected result:



          > dyn.load("a.so")
          > .C("double_me", x = as.integer(2))
          $x
          [1] 4





          share|improve this answer














          This is because you are using a.cpp; C++ function names are "mangled" by the compiler. You can use your same code with the filename a.c, compiling it just as you did, and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Or, alternatively, you can add this line to the top of a.cpp:



          extern "C" void double_me(int* x);


          and get the following from R:



          > dinfo <- dyn.load("a.so")
          > .C("double_me",x=2)
          $x
          [1] 2


          Update: Why was the result above 2?



          If you do not coerce the argument to the proper type, a copy may be made, such that your original value is not altered; if we coerce the value to be an integer as we should when using .C(), we get the expected result:



          > dyn.load("a.so")
          > .C("double_me", x = as.integer(2))
          $x
          [1] 4






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 at 20:28

























          answered Nov 11 at 15:11









          duckmayr

          6,84311126




          6,84311126











          • This works! Thank you very much!
            – Alexander Sobolev
            Nov 11 at 15:14











          • $x --> [1] 4 (typo?)
            – Alexander Sobolev
            Nov 13 at 20:12






          • 1




            @AlexanderSobolev Not quite; I've add some info to explain
            – duckmayr
            Nov 13 at 20:28
















          • This works! Thank you very much!
            – Alexander Sobolev
            Nov 11 at 15:14











          • $x --> [1] 4 (typo?)
            – Alexander Sobolev
            Nov 13 at 20:12






          • 1




            @AlexanderSobolev Not quite; I've add some info to explain
            – duckmayr
            Nov 13 at 20:28















          This works! Thank you very much!
          – Alexander Sobolev
          Nov 11 at 15:14





          This works! Thank you very much!
          – Alexander Sobolev
          Nov 11 at 15:14













          $x --> [1] 4 (typo?)
          – Alexander Sobolev
          Nov 13 at 20:12




          $x --> [1] 4 (typo?)
          – Alexander Sobolev
          Nov 13 at 20:12




          1




          1




          @AlexanderSobolev Not quite; I've add some info to explain
          – duckmayr
          Nov 13 at 20:28




          @AlexanderSobolev Not quite; I've add some info to explain
          – duckmayr
          Nov 13 at 20:28

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250010%2ferror-calling-c-function-from-r-library-loads-but-function-is-not-on-the-ta%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto