How do you run and compile C++ code on alpine linux? - c++

I’m new to alpine linux, and I’ve been trying to use it to run and compile C++ code, but I can’t get it to run the code.
First, I put the code in the opt folder, than I did this:
cd / → cd opt → clang++ -S -emit-llvm myFile.cpp
That created myFile in the opt folder, then I tried to use
sh myFile
To run the code, but it said “no such file or directory found,” If I do sh myFile.cpp, I get a few errors.
What am I doing wrong here?
Some of the packages I have installed are build-base, and clang.

By default, the result of clang (clang++ as well) is saved to a.out file, so you may run ./a.out or sh a.out. If you want to change output, use -o <filename> in clang command

Related

Where are GNU Make's implicit rules defined? How can I change them on macOS?

I wrote some C++ code in Windows. When I run make fileName from the Visual Studio Code terminal I see the command that make runs is g++ fileName.cpp -o fileName.
I then compile the same file in macOS Catalina using the same command make fileName and the command that make runs is c++ fileName.cpp -o fileName. I have run make -p -f /dev/null and saw that the CXX variable is set to c++ which is an alias to clang.
I would like make to use g++ on macOS as it does on windows.
I have seen posts mentioning you can change the implicit variables but haven't figured out how to do it. I already changed the default compiler in VS Code from usr/bin/clang to usr/bin/gcc and also tried with usr/bin/g++ but the command doesn't change to g++ fileName.cpp -o fileName as desired.
I have also tried running CXX=g++, and CXX='/usr/bin/g++ in the terminal but that didn't work.
Is there a way to do it? I would like to not have to create a makefile in my directory and instead just change the implicit rule.

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

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

C++ code compiles, but shows an error when running on Zorin OS

I'm learning C++ and trying to run a simple hello world program. It compiles but it won't execute. It worked on Windows, but it won't run on Zorin OS.
I read online that the command to run it is ./test or ./test.exe.
This is what is looks like on the terminal:
$ g++ test.cpp -o test.exe
$ ./test
bash: ./test: No such file or directory
I looked at the questions similar to this, but none have helped me.
You can not expect to be able to execute the same commands on both Windows and Linux. They use different shells with different syntax and different behaviors.
Here's a typical example of compiling a file on GNU/Linux:
dir$ g++ myfile.cpp -o myfile
dir$ ./myfile
Here's a typical example of compiling the same file on Windows:
dir> g++ myfile.cpp -o myfile.exe
dir> myfile
Note in particular:
Linux doesn't use .exe or other extensions on executables, but Windows does.
Windows doesn't require specifying directory to run files in the working directory, but Bash on GNU/Linux generally does.
The only reason why the compilation command is as similar as it is is that g++ is a Unix tool ported to both platforms. Windows normally uses / instead of - for flags like -o
As commands get more complex, they start diverging even further.

"make[2]: g++: Command not found" in Netbeans

I get the error message:
make[2]: g++: Command not found
which I know means it cannot find the C++ compiler. However, in Netbeans if I go to the configuration to choose my C++ compiler it shows the following being in /usr/bin:
g++4.6
g++4.7
g++4.8
but it doesn't have just g++.
Tried sudo aptitude install g++ but it didnt work.
If I do g++ --version I get:
The program 'g++' can be found in the following packages:
* g++
* pentium-builder
1) Verify you can run "g++" from the command line,
2) type whereis g++ to get the path,
3) Make sure you have the NetBeans C++ plugin installed
Go to Tools->Options->C++->Build Tools
4) Configure the path in NetBeans
Usually, /usr/bin/g++ is a symlink to some /usr/bin/g++-4.7 (or g++-4.8 etc...); just make it again (which is usually provided by the g++ virtual package on Debian or Ubuntu), e.g.
% sudo -s
# cd /usr/bin
# ln -sv g++-4.8 g++
Above % and # are shell prompts that you should not type.
Of course, don't forget the sudo apt-get install g++ etc...
BTW, you might put that link from $HOME/bin/g++ to /usr/bin/g++-4.8 and you don't need root permission for that.
(sometimes, these symlinks go indirectly thru /etc/alternatives etc...)
Your issue is a sysadmin issue; you should rather ask it (with a big lot more details) on askubuntu or superuser; it is off-topic on Stack Overflow.
BTW, NetBeans is not a compiler, but an editor (sometimes called IDE). You could use something better/simpler (e.g. emacs or vim) with e.g. make as a builder (to be run inside a terminal). Before using NetBeans ensure that g++ -v is working alone in some terminal

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