Compiling multiple C++ files. Calling a binary to run a code - c++

I have 2 cpp files(with one main function) in /home/misha/proga/c++again folder. I built C/C++: g++ build active task and modified it to compile all files in the folder above. Now, I need to add one more task to call a binary. I think I should add one more entry in "tasks" to finally be able to run a code. Where can I read about how to write this second task? I am new to programming. Is my approach correct to run this code contained in two files? I also do not know where this binary lies. Is it tasks file in .vscode folder ?
I use Ubuntu 19.10 and VSC 1.46.1

In Terminal,
cd /home/misha/proga/c++again
Let's suppose your two cpp files are mainFile.cpp and file2.cpp
If g++ (so GCC) was not installed in your system, you can install it by running this command on the Terminal:
sudo apt-get install gcc g++
and, to compile the program (read about invoking GCC, you want warnings and debug information), write this command into the Terminal:
g++ -Wall -g mainFile.cpp file2.cpp -o yourprog
Then, you can run the program by typing:
./yourprog
It should work now. You could need to use the GDB debugger and GNU make (to be installed with sudo apt-get install gdb make)
Read also some C++ programming book and this C++ reference.

I do not understand your approach usualy your create a makefile and compile your cpp files
g++ -g -c -fpic -o name.o
at the end you link them
g++ name.o 2name.o and so on
If you create binarys you should store them in /usr/lib
and the name should libname.so you can acces them by using the -l argument

Related

how to run bonmin from a cpp program

I’ve already downloaded Bonmin-1.8.8 and compiled it. The usersmanual said that there is a cpp example in the /bonmin/examples/cppexample.I added the bin and lib to my Path but when I coded
g++ mybonmin.cpp -o mybonmin
It shows that cannot find the headfile. I want to know how can I run Bonmin with the cpp program.
Try passing the flag to the headers:
g++ mybonmin.cpp -I/home/mybonmin -o mybonmin
here more info about it:
-I [/path/to/header-files]

Why in my Linux, when current work directory is owned by root (e.g /usr/bin), then I cannot link any library while compiling c++?

If I write this code and saved as a.cpp at ~/Desktop
#include <memory>
int main(){}
then input to bash:
cd /usr/bin
g++ -g ~/Desktop/a.cpp -o ~/Desktop/a
then the g++ will output plenty of messy code of errors.
I have found the reason is because it don't have authority to link XX.so library.
But if I add a 'sudo' , or set CWD to the path owned by user, g++ will work properly, as follows:
sudo g++ -g ~/Desktop/a.cpp -o ~/Desktop/a
or
cd ~/Desktop
g++ -g ~/Desktop/a.cpp -o ~/Desktop/a
Why do this happen? or how can I fix this?
You don't want to generate code directly in /usr/bin.
You generate your code in your user folder, maybe create a sub-directory called cppwork or something like that.
cd
mkdir cppwork
cd cppwork
g++ -g a.cpp -o a
Once you compiled in your directory, then you copy the file using install which will also take care of stripping the debug if any (i.e. the -g says to keep debug info—stripping is not mandatory).
sudo install -s a /usr/bin/a
As you can see, the place where I use sudo is with the install command.
That being said, I never use those directly. Now a day, I use cmake which means everything works automatically. But that would be a different discussion.
Thanks for every one. I have found the reason. It's because there is an executable program named 'array' in /usr/bin. And when CWD is /usr/bin, the compiler regard this 'array' as the c++ header <array>, so compiling error.
Then I need to find out why the compiler includes /usr/bin by mistake.

How to set up a make file with environment variable $BOOST_HOME

I need to setup a make file to compile my code with the environment variable $BOOST_HOME pointing to the boost install directory. Currently I am compiling using the command
g++ -std=c++0x -Wno-deprecated main.cpp
How would I go about doing this, so that my code will compile for others if their boost install directory is different from mine?

How to create alpha_encoder.exe (webm-tools) under msys2?

I'm trying to compile alpha_encoder) (little utility of The WebM Project, under webm-tools).
I have a previous installation of msys2 (downloaded and configured by build_locally_with_various_option_prompts.bat) under c:\FFcompiler. It took its time, but I managed to compile ffmpeg, so I decided to use it (I think it will do). That's what I've done till now.
First, I cloned webm-tools under /cygdrive/c/FFcompiler/ffmpeg_local_builds/sandbox/win32/libvpx-1.4.0/third_party/. There's a Makefile so I tried to run make:
$ cd /cygdrive/c/FFcompiler/ffmpeg_local_builds/sandbox/win32/libvpx-1.4.0/third_party/
$ git clone https://chromium.googlesource.com/webm/webm-tools.git
$ cd webm-tools/alpha_encoder/
$ make
But g++ complains mkvparser.hpp doesn't exist. The command is
g++ -c -W -Wall -O3 -g -I../../libwebm alpha_encoder.cc -o alpha_encoder.o
After searching the web, it seems that webm-tools depends on libwebm, and expect finding it as a sibling folder of webm-tools. So...
$ cd ../..
$ git clone https://chromium.googlesource.com/webm/libwebm.git
$ cd libwebm
Now, what? README.libwebm tells that 'to cross compile libwebm for Windows using mingw-w64' first I must run cmake like this cmake -DCMAKE_TOOLCHAIN_FILE=path/to/libwebm/build/mingw-w64_toolchain.cmake path/to/libwebm. In my case:
cmake -DCMAKE_TOOLCHAIN_FILE=build/mingw-w64_toolchain.cmake .
And cmake cannot find i686-w64-mingw32-g++. After googling more, it seems the easiest way to fix this is to add bin of mingw-w64-i686 to PATH.
$ export PATH=/cygdrive/c/FFcompiler/ffmpeg_local_builds/sandbox/cross_compilers/mingw-w64-i686/bin:$PATH
After this, now cmake finishes successfully and creates a Makefile, but make stops with an error:
/cygdrive/c/FFcompiler/ffmpeg_local_builds/sandbox/win32/libvpx-1.4.0/third_part
y/libwebm/common/file_util.cc:44:39: error: 'tmpnam_s' was not declared in this
scope
errno_t err = tmpnam_s(tmp_file_name);
^
I've searched about the error but I'm stuck. What am I missing?

Build a Linux executable using GCC

I'm using Ubuntu 8.10 (Intrepid Ibex) and compiling C++ files with GCC, but when I compile, gcc makes an a.out file that is the executable. How can I make Linux executables?
That executable is a "Linux executable" - that is, it's executable on any recent Linux system. You can rename the file to what you want using
rename a.out your-executable-name
or better yet, tell GCC where to put its output file using
gcc -o your-executable-name your-source-file.c
Keep in mind that before Linux systems will let you run the file, you may need to set its "executable bit":
chmod +x your-executable-name
Also remember that on Linux, the extension of the file has very little to do with what it actually is - your executable can be named something, something.out, or even something.exe, and as long as it's produced by GCC and you do chmod +x on the file, you can run it as a Linux executable.
To create an executable called myprog, you can call gcc like this:
gcc -c -o myprog something.c
You could also just rename the *.out file gcc generates to the desired name.
That is the executable. If you don't like a.out, you can pass an -o flag to the compiler. If the executable isn't marked with an executable bit, you need to do so yourself:
chmod u+x ./a.out
./a.out