How to launch a macos .app from C++ (nonblocking)? - c++

What's the best way to launch a macosx .app from a C++ program, without blocking the C++ program?
ie given a macos x app at path:
/somewhere/foo.app
How can I within a C++ program:
int main() {
run_mac_app("/somewhere/foo.app"); // returns immediately
// after main exits, foo.app continues running.
}
How do I implement run_mac_app?

I guess this is what you need to run in the terminal:
open -a /somewhere/foo.app
If you want this app to run in the background, then you have to add -g
open -g -a /somewhere/foo.app
So Finally,
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
std::system("open -g -a /somewhere/foo.app");
}
Reference from:
https://en.cppreference.com/w/cpp/utility/program/system
https://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html

Related

C++ code run in debug console rather in terminal in vs-code

I wanted to execute code in terminal not in debugger
After downloading all the c/c++ extension in vs-code.
code
#include<iostream>
using namespace std;
int main()
{ int UserInputOccur;
cout<<"helloworld";
cin>>UserInputOccur;
cout<<UserInputOccur;
return 0;
}
All the compilation and debugging done in debug console only till
( First user input i.e When first cin>>UserInputOccur; and then the code in terminated with helloworld1020=thread-exited,id="3",group-id="i1"
If you are using Linux you can write your code in a file, such as main.cpp.
Then you can open terminal -> go to the directory of the file -> run command g++ main.cpp -o main.
Now you can run your code on terminal with command ./main

Cannot run g++ compiled program Ubuntu

I created a simple program
#include <cstdio>
int main(){
printf("Hello world!");
return 0;
}
and compiled it with CodeBlocks 13.12 on Ubuntu 16.04, successfully
I am trying to run it from bash nothing happens
What am i doing wrong ?
test is an intrinsic bash shell command, so instead of calling your executable program you're calling this one.
To call you own executable test refer to the local binary in the directory it was created. Either ./test, or ~/Programs/test/bin/Release/test.

Getting Environment Variable $PATH in Linux using C++ in Eclipse

I am trying to get the value of environment variable $PATH in Linux using a simple C++program as bellow:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
// get PATH using pipe
FILE* pip = popen("exec bash -c 'echo $PATH'", "r");
if (!pip)
{
printf("can not open pipe!");
return 1;
}
char lineversion[600];
memset (lineversion, 0, sizeof(lineversion));
if (!fgets(lineversion, sizeof(lineversion), pip))
{
printf("fgets error!");
return 1;
}
std::cout << lineversion << std::endl;
// get PATH using getenv
char* pPath = getenv ("PATH");
std::cout << pPath << std::endl;
}
I used two different methods: using pipe and using getenv method. They both output this:
/opt/texbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/netbeans-7.3.1/bin
Interestingly the actual value of PATH is something different!
Why my C++ program is showing a different value for PATH?
Edit 1: I am running my C++ program in Eclipse IDE.
Edit 2: Compiling the program directly (without Eclipse IDE) shows the correct PATH value!
Edit 3: I found the answer in here too.
A process inherits the environment from the process that created it.
This is how Linux works, along with many other operating systems.
If you launch a program from Eclipse, that program inherits Eclipse's environment.
If you launch a program from the shell, that program inherits the shell's environment, including the modifications to PATH you have in your init files.
Since Eclipse has inherited its environment from whichever process launched it, you should see the expected output if you launch Eclipse from the shell instead of through your desktop GUI.

How to run a linux executable from C++ stored in other folder

The terminal commands to be run are :
cd folder1/folder2
sudo ./runnable
I am thinking of a solution such as :
exceute.run("cd folder1/folder2","sudo ./runnable")
in my C++ code. Moreover this should be done as SUPERUSER. Please help !!
Why not just call your program using an explicit path?
For example, if your program is located in /projects/folder1/folder2,
then you would have:
#include <cstdlib>
#include <iostream>
int main()
{
std::cout<<"Starting runnable executable..."<<std::endl;
std::system("cd /home/youbot/applications; sudo ./runnable");
return 0;
}
chmod 775 /projects/folder1/folder2/runnable
Running your program as root is not recommended though.
See this link for more info on why it's not advisable to run a program using sudo.

c++ using system command problem

hi every body i have a strange problem i write a code in c++ complied it successfully and run it successfully. i compiled with following command
g++ 1.c -o abc
to run program i use ./abc
now my problem is that i write a another code in c++ like
#include <fstream>
#include<iostream>
using namespace std;
int main()
{
ofstream SaveFile("/home/hadoop/project/hadoop-0.20.0/conf/core-site2.xml");
SaveFile <<"<configuration>";
SaveFile<<endl;
SaveFile<<"<property>";
SaveFile<<endl;
savefile.close();
return 0;
}
now i want to run abc in this code how to do this ?
how to use or run abc in this file?
how to use ./abc in this program ?
Actually, your question title ("... using system ...") says it all. Use:
system ("./abc");
to run the ./abc program.
There are other ways to run programs from within a program (which usually depend on platform-specific features) but this is the most portable.
A full sample program, testprog.cpp, to show what I mean:
#include <cstdlib>
int main (void) {
std::system ("ls -ald m*");
return 0;
}
Compiling this with:
g++ -Wall -Wextra -pedantic -o testprog testprog.cpp
and running the resultant executable testprog, this outputs (on my Ubuntu 10.04 box):
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder
In other words, it runs the ls -ald m* command from within the program itself.