Netbeans - run bash script before running the program? - c++

might be a stupid question, but I've just started using Netbeans 10 for a C++ project.
When I run/debug the project, I'd need to first execute a bash script which sets up the environment, and then execute the binary (including an argument to be passed):
#!/bin/bash
source setup_script.sh
./bin/my_program -c some_argument
I've tried just pointing Project Properties -> Run -> Run Command to the .sh file containing this launch script, but that won't do it (the Run / Debug buttons are either greyed our, or it'll tell me it doesnt recognize the executable type).
Is there any way to setup Netbeans to launch my application via this script, or any other way to achieve the same - e.g. setting up some sort of pre-run command that'd execute setup_script.sh in the same session?
Thanks!

Related

Source environment variables before running cmake in VSCode

I am trying to build a large C++ framework in VSCode using CMakeTools and C/C++ Extensions in Visual Studio Code. I am browsing stackoverflow/github issues/any google suggestion and it looks like I am not the first person to encounter this, but I can not figure out for the life of me what am I doing wrong.
Here is the problem. I want to setup VSCode in a way to be able to build the framework (it is C++) right from VSCode using the built-in tools/extensions. Here is the process I was using up until now (in standard terminal in linux) and it also works in terminal run in VSCode:
cd /path-to-project-main-folder
source scripts/env.sh
cmake .
make -j 10
Now the problem is that when I set up VSCode, open the folder where the framework is, VSCode recognizes it is cmake project and gives me the opportunity to build it. Problem is that when I try to build it, it does not set up the environment first and therefore uses wrong cmake (not the sourced one but the default one build in server) and also wrong libraries and some of them are not even recognized.
The problem is in the first line:
source scripts/env.sh
where the environment variables are set and also PATHs to some libraries and programs. This line is not ran by VSCode before cmake and build.
Does anyone know a solution on how to configure CMakeTools extension to run:
source scripts/env.sh
line before running cmake and then make?
Thank you
I was looking into some solutions using tasks.json, settings.json files or creating my own kit. But no solution worked for me or I did not completely undestood the solution:
https://github.com/microsoft/vscode-cmake-tools/blob/HEAD/docs/tasks.md
https://github.com/microsoft/vscode-cmake-tools/issues/2243
https://github.com/microsoft/vscode-cmake-tools/pull/995
VSCode: Set environment variables via script
and man pages of CMakeTools, VSCode,...
VScode remote development: How can I run a build task to source environment variables before running the real build task? ---> but I use cmake
VSCode, how to source environment variable files like setup.bash?

How to use ~/.bash_profile environment variables when using "Run Script" in "Build Phases" for XCode 6.1?

I have a cross platform unit testing framework that I use for C++. In order to build and run the tests in XCode 6.1 I need to run a python script as part of a prebuild script.
Since I use the same unit testing framework for multiple projects I figured it'd be great to use environment variables in my ~/.bash_profile to point to the resources necessary for the python script. But it seems that XCode 6.1 doesn't load the bash_profile before running the script in the build process. Are there alternatives to using the bash_profile? Maybe I could add my environment variables to the XCode environment variables list? But this seems to be changing with each update to OSX. I can't seem to figure out where I can add environment variables for XCode 6.1.
You can run a shell script by adding a build phase to your target. The instructions are also here.
Select the project file
Select "Build Phases" at the top
Select Editor > Add Build Phase > Add Run Script Build Phase
Add your script in the box provided
in your case you can do:
source ~/.bash_profile
python [your_script]
You can rearrange the order of your build phases by dragging them into the order you want.
NOTE: The environment variables defined in ~/.bash_profile will not be there when you run your program (at least in my experience). To add environment variables to the run command:
Select Product > Scheme > Edit Scheme...
Select Run on the side bar
Select the Arguments tab
Put any Environment variables needed by your program in the proper section

Window appearance without style in C++ Qt application scheduled in Ubuntu

When I normally run my application from the command line it looks like this:
However, when I schedule the command using the crontab (or the "at" command), the window looks like this:
The command that I schedule is as follows: "export DISPLAY=:0.0 && myprogram". I use Ubuntu 12.04 64-bit.
Any idea how to get the nice appearance for the scheduled window? Thank you.
I think that when you run the program using the crontab the program is running with other environmet. Environment that don't have the configurations you already do.
For instance, in your user space you said QT theme that you want it looks like GNOME.
This can happend if the user root runs the app since root has a different environment, hence different setting for QT gui themes/appareance.
Possible solution, try to specify the environment which will run the app.
Another solution could be select the theme programatically, here you have a discussion about the subject.
Test: Try running the app yourself as root, and compare result. Don't use sudo. Try with su -c.
Program started from crontab has different environment. So you can create a shell script that sources your profile, for example using bash:
#!/bin/bash
source ~/.profile
export DISPLAY=:0.0
exec myprogram
And run that from crontab. If you do not normally use bash, you may need to use script with shell that you use.
Alternative would be to investigate your profile and find out, what difference in environment changes window appearance. Then export that variable in such script, or put it directly into crontab job, like you do for variable DISPLAY.

Command line parameters in netbeans

I want to run my program as if I was running it using a terminal. Because I want to do different things depending on what attributes the user of the program provides while running it. Is there any way to do that in netbeans?
See this tutorial which explains how to specify command line arguments for a C++ project in Netbeans 7.2.
It works very similarly to Java, in that you right-click the project, select Properties, then the Run page, then set the command line options you want to feed your application when running it in Netbeans.

Linux, Eclipse CDT run as su

I am using Eclipse CDT to write an application. The application uses a third part API, which requires to run as sudo, in order to open raw sockets, etc.
In command line, I can run the program something like
su
./program
But in Eclipse CDT environment, it won't work if I press Ctrl + F11 (Run->Run Last Launched), I guess the reason is that my Linux GUI login is not su.
Is there anyway that I can run as su (with su password) in Eclipse CDT?
Thanks.
Some of the options you have:
Run Eclipse as root. (Not a very good idea, imho, but the simplest one)
Temporarily enable your user to use that library. (Could be messy)
Create a new run configuration and make a script to run your executable.(You've to enter the password everytime).
(#Others, feel free to add more.)
You can create a run configuration in which you use sudo to run your application. Since sudo doesn't have access to a terminal if you launch it this way, it requires setting the SUDO_ASKPASS environment variable in your launch configuration. The steps are as follows:
Creating a new Run configuration (for the sake of completeness):
In eclipse, right click your executable (i.e. the result of the build of your application) to open the context menu
In the context menu, go to Run as -> Run configurations ...
In the Run configurations window that appears, right click C/C++ Application and click New.
Modifying the Run configuration
In the Run configuration's Main tab, replace the C/C++ Application field by
/usr/bin/sudo.
Go to the Arguments tab, and enter the path to your executable, e.g. ./bin/my-executable. Note: sudo's working directory is the eclipse project, so the path should be relative to that.
If your executable requires any commandline arguments, add those, e.g. ./bin/my-executable arg1 arg2
Adding a way for sudo to ask for your password.
This is the most tricky part. Since sudo does not have a terminal (tty) at its disposal when run from the (eclipse) gui, we need to provide it with a program that can obtain the password for it, i.e. an askpass program. For more info, see this stackoverflow answer.
On my system (Ubuntu 15.04), the package ssh-askpass-gnome provides an askpass program, as I found out by running dpkg --get-selections | grep askpass. Since that still didn't give me the executable name, I brute-forced the search by running
sudo find -name *askpass*. Anyway, if no such utility is installed, search for it using your favourite package manager.
Once we've installed and / or located an askpass program, we can continue creating our launch configuration in eclipse; continue from step 2.3
In the Run configuration window, select the Environment tab and click new.
In the dialog that pops up, enter SUDO_ASKPASS as name and the full path to the askpass program as value, e.g. /usr/bin/ssh-askpass.
Press Apply to save our changes.
Executing the launch configuration
In the Run configurations window, press Run to launch our executable using the newly created launch configuration.
A pop-up window for entering the sudo password will appear.
After succesful password entry, our executable will run with root priviliges.