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.
c++ r dll
add a comment |
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.
c++ r dll
add a comment |
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.
c++ r dll
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
c++ r dll
edited Nov 11 at 15:12
asked Nov 11 at 15:03
Alexander Sobolev
661311
661311
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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