Issues with creating a Mac DYLIB for system() function - c++

I am trying to write a DYLIB, (in Code::Blocks), which is to be called from a Mac application, that simply calls the standard C system() function, while giving the option of whether to wait for finishing, in the second argument, (1 or 0).
I put my sample code on pastebin, instead of embedding it here, because for whatever reason, the post editor is not letting me place the entire code snippet inside the code box, like it's supposed to, which is a little annoying.
http://pastebin.com/0ZfbD3Dx
Now when calling the DYLIB, (I can't remember the compiler I used, either GCC or LLVM), I use this code within GameMaker Studio:
ExecuteShell('open /Applications/Calculator.app',0);
However, calculator does not run. Trying to change the second argument to 1, and I get the same result. Calculator does not run. No errors. It seems to be ignoring my code all together, so I was wondering if there was something wrong with how I wrote my DYLIB.
I look at this question someone else had:
https://stackoverflow.com/questions/859517/osx-equivalent-of-shellexecute#=
string command = "open " + filePath;
system(command.c_str());
I see they used .c_str(). Should I be using .c_str()? Is that the problem? The code I use right now is system(fname);. Should I change that to system(fname.c_str());? Is it OK that I'm using the type char * for the argument fname? Or because I'm using that type, do I have to cast/convert it to the type string?
I'm using someone else's Mac to port this small project of mine to work on Mac, anyway I'm trying to get help on here, before trying to build it again, because I'm trying to minimize how much I'm asking to use my friend's Mac computer. The hope is, the next build I do, with very little tweak, will finally work and run calculator.
Thanks in advance!

Related

Is there a way to stop RGui from crashing when an RCPP program fails to work correctly?

I'm using Rcpp to run C++ code using RGui (version 3.4.1) as a user interface. Quite often I make changes to the C++ code which compile correctly but cause errors (e.g. searching beyond the end of an array) when I run the relevant program in RGui, causing RGui to crash. This is aggravating because I have to re-open RGui, re-open my R script (sometimes with unsaved changes lost), set the working directory again, etc. before I can re-compile the C++ code and run the program in such a way as to find the problem or test amendments. Sometimes it promptly crashes again because I haven't fixed or bypassed the problem.
Is there some way to change the way Rcpp runs such that RGui returns an error message instead of crashing in these sorts of situations?
Briefly:
It is spelled Rcpp. Capital R, lowercase cpp.
Yes, don't have bugs :)
In general, 2. is the only viable answer. If you need a managed language, use R.
If the code takes your environment down, test outside the environment. Seriously. That is for example why I (co-)wrote littler and test "raw code" on the command-line: it can only take the command-line app down.
We do have a feature in eg RcppArmadillo to test for "out of bounds" vector access: use x.at(i,j) which will warn. See http://arma.sourceforge.net/docs.html#element_access
I don't actually know of a way to prevent this apart from more careful programming, and saving before execution. But having done this a few times I have discovered a way to get back at unsaved changes, (at least in windows).
When you get the pop-up that tells you to restart R, you don't do it. You open up task manager and right-click on the process and select 'Create Dump File'. Find this file in explorer and open it with some text editor.
They are very big, and full of all sorts of stuff, but if you use find function to search for some string you know to be in your script, then you can find all the unsaved work. You can then copy and paste this into another file to save.
If you use R-studio instead of R-GUI, it usually manages to look after your unsaved work better.

Linux system call aio_write() fails with error code 22 (EINVAL)

I have a testing utility that uses linux aio_write and aio_read.
This testing utility wraps my static library and test it. This library is multi-threaded black box.
Up until now, it worked fine. But now we made a big change into the this black-box which causes the testing utility to fail as soon as it commits the first IO.
This IO returns with errno 22 == EINVAL.
Based on the aio_write man pages, this error is issued in case one of the following fields is invalid --> aio_offset, aio_reqprio, aio_nbytes.
I run it inside gdb and tested their values as long as with all other values inside the struct aiocb * input parameter.
My conclusion is that the input parameters are all valid.
I suspect something has changed in the way threads are working inside the black-box. This is what i suspect causing this issue (i can not find any other explanation).
What I really try to understand is: Which scenarios causes aio_write() to return a EINVAL error code???
Just to clarify, when I replace the black-box to an older version, using the same testing utility it works fine.... (i also tested the input arguments here as saw they are matching to the bad version input arguments).
You can take a look at the aio implementation in the linux source code at the folder linux-kernel-source/fs/aio.c
Sadly there are a lot of points where -EINVAL is returned. As #myaut mentioned in his comment, I recommend you to use strace. Another solution would be to modify the code, compile it and check where it fails.

getenv("HOME") returns "/root" with "sudo"

I'm writing a program on my Raspberry Pi that requires the function "getenv("HOME")" to locate "/home/pi".
However, since I'm using the "wiringPi" library that requires "sudo" to run, "getenv("HOME")" now returns "/root" as the HOME directory instead of "/home/pi".
Is there a way to locate "/home/pi" with "getenv("HOME")" while using "sudo" the run the program?
Any help will be appreciated. Thank you.
Transferring comments and response into an answer.
If you know the answer is /home/pi, why do you need getenv("HOME") to get the wrong value?
It's because getenv("HOME") is the code from another library I am trying to run, which I cannot change.
Presumably, calling setenv("HOME", "/home/pi", 1) is a bit too much like cheating, too?
setenv("HOME", "/home/pi", 1) works for me.
Why are you sure that the value you need is /home/pi? Why isn't /root correct when the program is run by root (or someone running sudo)?
This becomes mostly immaterial given that there's another unchangeable library involved.
In that case, setting the correct value for the environment variable before invoking the other library is a mostly reasonable mechanism.

iOS: execl(3) doesn't work anymore

I'm trying to call execl within my iOS application, which is running in the iPhone Simulator. But that fails. Xcode shows this error:
error: address doesn't contain a section that points to a section in a object file
When going step by step, this error pops up at the moment when I try to step into execl.
So, this makes me think that there is a problem with that very specific method. That it actually doesn't exist in the compiled executable? Do I need to link specific libraries? I tried this as well:
printf("execl(): %llx", &execl); // get the address of that method
Which prints values like: execl(): 3aeee0e. However, when debugging it states: EXC_BAD_ACCESS and Thread 1: 0x0000000.
Any ideas? I also tried methods like execve(), but give the exact same result. In the past, the exact same code work perfect.
For those who know what I'm talking about: Yes, I really want to use valgrind and not the tools by Apple.

C++ interpreter / console / snippet compiler

I am looking for a program where I can enter a C++ code snippet
in one window, press a button, and get output in another window.
Compilation should somehow be hidden behind the button. On a
per-snippet basis would be fine, full interactive probably asking
too much. It should run under Linux/Unix. Main use case would be
learning/testing/short debugging, etc.
Related stuff I found:
-- the Reinteract project for python (which i'm told sage has features similar to)
-- the same thread for C# here: C# Console?
-- the CINT interpreter from the CERN ROOT project
(which may be close, but maybe there are more comfortable apps around)
-- some programs called Quickly Compile or Code Snippet, which are M$.
http://codepad.org/ works nicely for this purpose. By default, it will run what you paste when you hit submit and display the result (or any errors you might have).
Dinkumware has a page for this AND you can choose the compiler
http://dinkumware.com/exam/default.aspx
Do something like this ?
test a C# snippet code by just copying it to the clipboard and then type csc.exe:
http://reboltutorial.com/blog/redirect-shell-to-rebol-console/
Cling (interactive C++ interpreter, built on the top of LLVM and Clang libraries): https://root.cern.ch/drupal/content/cling
Just configure your code editor to compile and run your code snippets.
Most code editors have the capability of 'sending' the current buffer/file to an external tool. I configure one editor key binding to compile the current buffer, and another key binding to execute whatever was last compiled (actually to run whatever has the same base filename as the current buffer with an '.exe' extension). My experience is with Windows, but the same or similar can be done on Unix/Linux.
Then it becomes one keystroke to compile and another to run what I jut compiled. This could also easily be just a single keystroke to compile & run, but I have several key bindings set to compile using various different compilers. That way I can easily test snippets using the latest MSVC, MSVC 6, MinGW GCC, Comeau and Digital Mars compilers to check for differences.
I would do it like this:
Capture the 'snippit' as text
Create a.cpp with the following:
int main() {
snippitCode();
return 0;
}
void snippitCode() {
// INSERT SNIPPIT HERE
}
Use 'exec' to launch a compiler and pipe the output to an output file.
Use 'exec' to run the application and pipe the output to an output file.
In the 'output' window, you can run 'tail -f' on the output file to continuously update when new output arrives.