Making c++ app use directory of file, not pwd - c++

I am trying to run an application, app. I do this by running ./app in the directory of app. This application has a dependency, graphics/file.bmp. Everything works when I run ./app in that directory.
If I instead run from the parent folder, it can't find graphics/file.bmp when I run ./app_directory/app
What is the cleanest way to resolve this? I would like to cd into the directory of the file no matter where I am running the program from. I am on OSX and would be thrilled by a solution that works across all unix machines.

You can do this on the first line of main():
chdir(dirname(argv[0]));
dirname() removes the last path component from the input ("a/b/c" turns into "a/b") and if there is only one path component then it returns ".". Passing this return value directly into chdir() will change the working directory to the directory containing the binary.

You could try writing a bash script that you could run from any directory and it will cd into the directory that successfully runs the program, but this won't actually change the working directory at which your terminal is sitting. In case you don't know how to do that: make a file called build (or whatever you want) and place it in the parent folder. In that file put the following code:
cd app_directory
./app
After you create the script run chmod u+x <script-name>, which will make it an executable. Once this is done you can just run ./<script-name> and it should run your program just fine.

Related

How to pass in image to C++ executable and store output image in a new directory?

I'm trying to write a program in python wrapper for a C++ executable. The executable takes in an image and returns an edited image. I've looked into using subprocess.run, in this way:
output_img = subprocess.run(["executable","argument"], shell=True)
However, this gives me the error of "executable: command not found." Also, the executable file does not have .exe at the end, and I'm not sure why because it was given to me by someone else.
I simply want to run an image through this executable and take the output image and store it into a new folder. Any ideas how to make this work? Thanks!
First try and run it yourself in Terminal. So, to find out if it is a suitable binary to run on your system, run:
file executable
and click edit under your question and paste the output. Then make sure the executable bit is set with:
chmod +x executable
Then try and run it (if you trust it) with:
./executable someImage.jpg
Then see the most recently created file in your directory to see what it output - it will be the last one listed if you run:
ls -lrt
If that all works, change your Python to:
output_img = subprocess.run(["./executable","argument"], shell=True)

F# package for Sublime Text Build System absent

I am starting out with F# and trying to get it to work with Sublime Text 3 with a package, https://github.com/fsharp/sublime-fsharp-package. After installing the package using Package Control, I see F# appear as an available language to use in Sublime Text's bottom bar, and syntax highlighting appears to work more or less, from what I can tell, but the build system for F# fails to appear as it should.
So, trying to fix things, I run "build.sh install" and get an error, "Cannot open assembly '.paket/paket.bootstrapper.exe': No such file or directory." I am sort of stuck. Many thanks for any help.
From the comments you've made, you appear to be a little unfamiliar with the Unix underpinnings of OS X. I'll explain those first, then I'll suggest something for you to try that may fix your problem.
Technically, files or directories whose name starts with . are not "reserved for the system" as you put it; they're hidden. Now, it's true that Finder won't allow you to create files or directories whose name starts with ., because Apple didn't want to have to field all the tech-support calls from people who didn't know about the hidden-files feature: "I named my file ... more important stuff for work and now it's gone! Help!!!" But if you're in the Terminal app, then you can easily create files or directories with . as their first letter: mkdir .foo should work. You won't see it when you do ls, but ls -a (a for "all") will show you all files, including hidden files. And you can also do cd .foo and create files inside the hidden .foo directory -- and while the .foo folder won't show up in Finder, it will be perfectly accessible in the Terminal, and to any F# programs you might write.
So when you say that you cloned https://github.com/fsprojects/Paket but it failed to include the .github and .paket directories, I think you just don't know how to see them. You can't see them in the Finder (well, you can if you jump through a couple of hoops but I don't think it's worth the effort), but you can see them with ls -a. Just open your terminal, run cd /Users/Username/Paket, and then run ls -a and I think you'll see that the .paket and .github directories were indeed created by your git clone command.
So what you should probably try is this:
Go to https://github.com/fsprojects/Paket/releases/latest
Download the paket.bootstrapper.exe and paket.exe files. Put them in /Users/Username/Downloads (or wherever the default OS X Downloads directory is if it's different -- just as long as it's somewhere where you can find them easily).
Open the Terminal app.
Go to the directory where you've unpacked the Sublime Text 3 package. I.e., in the Terminal app, run cd /Users/Username/Library/Application\ Support/Sublime\ Text\ 3/Packages/sublime-fsharp-package-master.
Run ls -a and see if there's a .paket directory.
If it does not exist, run mkdir .paket.
Now do cd .paket so you're in the hidden .paket directory under sublime-fsharp-package-master.
Now do ls and see if there's a paket.bootstrapper.exe file.
If it doesn't exist, then copy in the .exe files you downloaded earlier:
cp /Users/Username/Downloads/paket.bootstrapper.exe .
cp /Users/Username/Downloads/paket.exe .
Important: Now do cd .. to go back up to the /Users/Username/Library/Application\ Support/Sublime\ Text\ 3/Packages/sublime-fsharp-package-master/ directory.
Now instead of running /Users/Username/Library/Application\ Support/Sublime\ Text\ 3/Packages/sublime-fsharp-package-master/build.sh install, try running it as ./build.sh install. (And also try ./build.sh Install, since I'm pretty sure the capital I is necessary).
(BTW, If you're not familiar with the syntax that I used in steps 9, 10 and 11, where I used a single . or two dots .. in commands, those are a long-standing Unix idiom: . means "the current directory", and .. means "the parent directory".)
I just looked at the build.sh script that you've been running, and it seems to assume that you've done a cd into the package's base directory (the sublime-fsharp-package-master directory) before running the script. So that could explain why it was failing: you were running it from a different directory, rather than doing a cd first. Hence why I marked step 10 as important: I think that was the root cause of the problem.

How to install ninja-build for C++

https://github.com/ninja-build/ninja/releases
I have downloaded the ninja-win.zip folder and extracted it. When I open it, there is a single .exe file in the entire folder. When I double click it a cmd window flashes for a split second. I have also tried running it as administrator, but the same thing happens. What I don't understand is, what am I expected to do with this .exe file?
You must open a terminal (cmd.exe on Windows) and type something like ninja -f /path/to/buld/file. You may also wish to modify the PATH environment variable so that Windows knows where to find the Ninja executable, depending on your setup.
You can simple download ninja.exe file from this Link
https://github.com/ninja-build/ninja/releases
After that you just have to add the path to your ninja.exe file to your windows environment variables and then you can use ninja commands from anywhere in windows.
1. Open cmd in your Project Directory
2. There are guides on the internet on where to save the Ninja.exe so that it'll be callable in Cmd without specifying directory. Either follow them or:
i, Specify Directory when Calling Ninja. Putting "ninja" in Cmd actually calls Ninja.exe and is the same as something like "C:\users\user1\downloads\Ninja". or:
ii, Save Ninja.exe in the same directory as Project.
3. proceed with rest of the command.
Therefore the Final Command would be:
"C:\users\user\downloads\Ninja.exe" -f "D:\Projects\Project1"

AWS CodeBuild directory issues?

Using the stock CodeBuild Ubuntu14.04 base image.
Anyone else experiencing odd behavior with directories? Commands like "pushd" do not exist. You start out in this weird "/tmp/src200838814/src" directory. The cd command seems wonky too.
Really hard when you are trying to run cmake.
The way AWS CodeBuild works is that each command is executed in the base directory. For your case all commands will be executed in "/tmp/src200838814/src". If you would like to execute commands in a different directory you will have to chain a single command to move you then execute for example.
cd other-dir && cmake
Let me know if you have any other questions.
Did you try with this env CODEBUILD_SRC_DIR?
I always cd to this one before executing anything to make sure I know what is my current directory.
cd ${CODEBUILD_SRC_DIR}

C++ system mkdir with path

Ran into a little snag here. I'm trying to make a directory inside of another directory using a variable directory name created by the function in use. Basically I want to store any created accounts in a directory named accounts that is separate from everything else. Here is what I have for my function:
system(("mkdir -p /home/user/Program/accounts"+accname).c_str());
The problem I am running into is that it creates the directory in Programs as accounts(accname) instead of in accounts with accname being the directory.
Example with accname = tim would currently look like accountstim inside of Program instead of tim inside of accounts.
You're passing the -p flag, which will create all directories that you don't already have, so you're on the right track.
You'll need to add another slash to get a new directory. Without this extra slash, anything at the end of the string becomes part of the accounts directory, and not the name of a new directory:
system(("mkdir -p /home/user/Program/accounts/"+accname).c_str()); // note the slash after accounts!
That would solve your problem, but I advise against using the system function
EDIT: Using mkdir only applies if you are running a POSIX system or other system that supplies a mkdir function. If you're on windows I don't know how that would be done.
It's advisable to use the mkdir system call instead. If you're only creating one directory, the mkdir function call should be relatively straightforward. If you are running Linux you can read about it here.