Xcode 4 file input/output, command line tool C++ - c++

I'm trying to figure out where to save multiple .txt files so that i can have a command line tool project in Xcode read directly in from them while running it.
I understand that Xcode compiles everything to a folder, DerivedData, which i have saved in the same location as my source code for each project respectively.
can i save multiple .txt files anywhere in the DerivedData folder or include it in the build settings and phases so that when i run the command line tool i can type the name of a file, it will read in from that file.

By default the compiled/linked binary will look into its own directory for files.
For example, my binaries are at ProjectName/Build/Products/Debug/ and therefore it will look for files from that dir.
You can use relative path from that folder to the outside.
Or, you can create a symbolic link to another directory (on Terminal):
ln -s source_dir target_file
target_file must be located in the same directory as your binary. And you can reference the other files like "target_file/file1.txt", etc.

Related

Developed clangtool does not find headers outside source tree (runtime)

I'm developing a clang based tool with which I process the AST generated by clang. Right now I'm developing inside the llvm-project tree (I cloned the whole repository) as mentioned in this tutorial. I've created the CMakeLists.txt as mentioned and I executed the cmake command as seen in the tutorial.
The project compiles and the output binary file is located in the build/bin with the other binaries inside the llvm-project source tree. When I execute my binary from the build/bin, upon parsing the input c++ file, everything goes well.
But, when I copy the generated binary to my home directory or anywhere else and I execute it, upon the parsing, it does not manage to find the standard headers for instance stddef.h:
/usr/include/stdlib.h:32:10: fatal error: 'stddef.h' file not found
My question is why it can find the header in one place and not in another? In one of the forum I found this: Some header files (stddef.h, stdarg.h, and others) are shipped with Clang — these are called builtin includes. Clang searches for them in a directory relative to the location of the clang binary.
I've checked the build directory, in which the binary is located originally, and it contains lib/clang/include directory in which there is also the file stddef.h. Is it possible that by executing in place the binary, it founds this path and when I copy it somewhere else it does not find the right path?
EDIT:
The command I execute is:
build/bin/mytool source.cpp -- -I(list of headers without system headers)

How do I specify where the dsc, build, changes, update, tar.gz files go when running debuild?

Once in a while I like to send a version of my software to my Ubuntu PPA on Launchpad. To do so, I need to regenerate the source, description, and change files. To do that, I run the following command:
debuild -S -sa -nc -m"alexis#example.com"
The problem is that it spits out all the files one directory up from my main folder. So say I am working on a project named Beautiful, I would see:
projects/
projects/Beautiful/
projects/Beautiful/<source files from my git>
projects/Beautiful_1.2.3~xenial.dsc
projects/Beautiful_1.2.3~xenial_source.build
projects/Beautiful_1.2.3~xenial_source.changes
projects/Beautiful_1.2.3~xenial_source.ppa.upload
projects/Beautiful_1.2.3~xenial.tar.gz
I would like to place all of the source files in a sub-folder such as:
projects/
projects/Beautiful/
projects/Beautiful/<source files from my git>
projects/Beautiful_source/Beautiful_1.2.3~xenial.dsc
projects/Beautiful_source/Beautiful_1.2.3~xenial_source.build
projects/Beautiful_source/Beautiful_1.2.3~xenial_source.changes
projects/Beautiful_source/Beautiful_1.2.3~xenial_source.ppa.upload
projects/Beautiful_source/Beautiful_1.2.3~xenial.tar.gz
Is it possible from the command line or a rule somewhere? Or is the one directory up the only place where debuild will work on that?

C++ Executable Error: cannot open shared object file: No such file or directory

I have a project path where I have created an executable testsd:
Caspian#Caspian-VirtualBox:~/TestProject/build/linux/debug/bin/testsd
The project directory and heirarchy is as following:
/TestProject
|-build/linux/debug/bin
|-ExtLib/folder/lib(containing .a and .so files)
|-ExtLib/folder/src(containing multiple sub folders with .cpp files)
|-ExtLib/folder/include(containing multiple sub folders with .cpp files)
|-src(containing multiple sub folders with .cpp files and mainc.pp)
|-tests(containing Runtests.cpp and catch.hpp files)
The problem is when am running this executable (./testsd), I am encountering the following error:
./testsd: error while loading shared libraries: libuastackd.so: cannot open shared object file: No such file or directory
The libuastackd.so files is contained in ExtLib/folder/lib and ExtLib/folder/src/stack/lib folders. Can anyone help, how can I overcome this problem?
Thanks rG
You might set the LD_LIBRARY_PATH environment appropriately.
See this. Read ld-linux.so(8).
There is some way to set LD_LIBRARY_PATH for your entire session, e.g. by editing appropriately ~/.login or ~/.bashrc or ~/.bashenv or ~/.profile etc ... and this would alter the behaviour of any program you start after, including some Eclipse IDE
Read Drepper's How To Write Shared Libraries, notably for other solutions -e.g. appropriate -Wl,-rpath settings (which is probably what you really should use).

Full path of *.cpp files

I need to have full path of C++ files (name of file is not neccesary). It musn't to be depended on path of execution.
I can use __FILE__, hovewer it gives me only name of file. I was seeking in boost::filesystem, but I only found boost::filesystem::current_path() - unfortunately it gives me path of directory from which I run program.
I looking for path of cpp file, not path of exe. I need it for other tool to generate groups of cpp files after directory of cpp files.
Any ideas?
Edit:
Maybe it is possible to use bash script which gives actual directory of cpp file (not sh file)?

Different paths used for #include and other files

I'm quite confused about this weird behaviour of my .cpp project. I've got the following folder structure:
include/mylib.h
myproject/src/eval.cpp
myproject/data/file.csv
myproject/Makefile
In eval.cpp I include mylib.h as follows:
#include "../../include/mylib.h"
and compile it through Makefile:
all:
g++ -I include ../include/mylib.h src/eval.cpp -o eval.out
Now in my eval.cpp I'm reading the file.csv from data directory and if I refer to it like this
../data/file.csv
it doesn't find it (gets empty lines all the time), but this
data/file.csv
works fine.
So, to include mylib.h it goes two directories up (from src folder) which seems right. But it doesn't make sense to me that to refer to another file from the same piece of code it assumes we are in project directory. I suppose it is connected with Makefile somehow, but I'm not sure.
Why is it so?
EDIT: After a few thing I tried it seems that the path which is used is not the path from binary location to the data location, but depends on where from I run the binary as well. I.e., if I have binary in bin directory and run it like:
./bin/eval.out
It works with data/file.csv.
This:
cd bin
./eval.out
works with ../data/file.csv.
Now it seems very confusing to me as depending on where I run the program from it will give different output. Can anyone please elaborate on the reasons for this behaviour and if it is normal or I'm making some mistake?
It is so because (as explained here ) the compiler will search for #included files with quotes (not with brackets) with the current working directory being the location of the source file.
Then, when you try to open your .csv file, it's now your program that looks for a file. But your program runs with the current working directory being myproject/ which explains why you must specify data/file.csv as your file path, and not ../data/file.csv. Your program does not run in your src folder, it will run in the directory the binary ends up being invoked from.
You could have noticed that in your Makefile, your -I options specify a different path for your header file than your .cpp file.
EDIT Answer: It's quite simple actually and completely normal. When you invoke your binary, the directory which you're in is the current working directory. That is, if you run it with the command ./myproject/bin/eval.out, the current working directory is . (e.g. /home/the_user/cpp_projects). My post was a bit misleading about that, I corrected it.
Note: You can use the command pwd in a command prompt to know which is the current working directory of this prompt (pwd stands for "print working directory").