Error running C++ file using Visual Studio in Ubuntu - c++

I am a beginner to Ubuntu and Linux. I'm attempting to run just a simple hello world code and every time I run the C++ code in visual studio, it doesn't actually run, it opens to a new file called settings.json (which is empty).
If anyone can help me sole this problem that would be great, thank you.

I am a beginner to Ubuntu and Linux.
Read about the Unix philosophy.
It is favoring the command line and combination of simple tools
(e.g. in a command pipeline for your unix shell in some terminal emulator). A successful command (e.g. cp(1), used to copy files, or g++(1), a C++ compiler, or man(1) to read documentation, and od(1) or less(1) to inspect a file, or ls(1) to list them) often stays nearly silent when successful; see intro(1). Be aware of syscalls(2) (see also intro(2)). Remember that some parts of your C++ code could be generated (by metaprogramming tools such as ANTLR, swig, or GNU bison or GNU autoconf or GPP, or your own Guile, Python or GAWK or GNU bash script, or some other C++ program, etc...). See also Linux From Scratch. Every executable and process (except /sbin/init) is started by execve(2) with fork(2). See also ps(1), top(1), pstree(1) and proc(5).
Your C++ compiler could be GCC (or else Clang). Be sure to read the documentation on invoking GCC, and about your C++ preprocessor (perhaps GNU cpp). Try g++ --version then g++ --help in some terminal emulator.
If that command works, compile your HelloWorld.cpp with all warnings and debug info, so run in your terminal g++ -Wall -Wextra -g HelloWorld.cpp -o HelloWorld; you later run the obtained executable using ./HelloWorld in the same terminal (read about the $PATH variable in environ(7) and try the printenv(1) command).
Of course, you'll use some IDE or source code editor (e.g. Visual Studio Code, vim or GNU emacs or geany). Be sure to take some time to read its documentation. You'll configure them to run some build automation tool. You surely want to use some version control system, such as git.
And you'll need a debugger such as GDB.
Later, you'll want to use some build automation tool to drive your compilation commands (of several translation units) and the linking command (part of GNU binutils). Consider for your build automation using GNU make or ninja or many others.
Read of course Advanced Linux Programming and some good C++ programming book (and reference website). Be aware that C++ is a very difficult and complex programming language (see its spec n3337).
You could enjoy reading some textbook on operating systems. Study for inspiration the source code of existing open source C++ programs (e.g. on github), such as the fish shell.

You cannot just run the file. The simplest way is to open the command terminal and find where your file is located, the path that it is stored in and type in g++ fileName.cpp, make sure to name it with a cpp extension.

It is possible to compile and run C/C++ programs from Visual Studio Code.
To compile and run C/C++ programs from Visual Studio Code (vscode) you need to install C/C++ Compile Run extension from danielpinto8zz6 in Visual Studio Code.
After installing the C/C++ Compile Run extension press F6 to compile and run C/C++ Program.
Hope it helps !

Related

Use Shared Object (.so) in a C++ project develop in Windows with Visual Studio for Linux using WSL

For a task I have I must develop C++ code for Linux but from Windows. I'm using Visual Studio 2019 with WSL 2 and the tests I've run worked well, it compile .out/.so/.a and giving me the ability to debug the code as if it was a regular project.
The problem is when I need to link external .so to my project. In regular Windows-VS-c++ development I define the input linker to the directory of the .lib. But in Linux their no .lib (or is their? I'm not familiar with Linux-C++).
In short how do I consume Shared Object (.so) files from another project using Visual Studio 2019 compiling for Linux using WSL 2 ?
Ask your boss permission to install Linux on your work computer
(e.g. some genuine Linux distribution such as Debian or Ubuntu in some separate disk partition). This is -in terms of effort and your time- the cheapest route to follow.
Read of course Advanced Linux Programming, syscalls(2), How to write shared libraries, dlopen(3), proc(5), elf(5), ld.so(8), the Program Library HowTo, the C++ dlopen minihowto, the documentation of GCC, the documentation of GNU make, the documentation of GNU binutils, the documentation of GDB.
Of course, read more about programming in C++. C++ is a difficult language (on both Linux and Windows). Refer to this website. Read later the C++11 standard n3337.
If you code a single C++ translation unit foo.cc (you could use GNU emacs to edit it), compile it first into a shared object foo.so using a command g++ -Wall -Wextra -g -fPIC -shared foo.cc -o foo.so (all warnings, DWARF debug information, position independent code).
If your C++ shared library is built from several C++ translation units, learn how to use a build automation tool such as GNU make or ninja. And use it in a terminal emulator on the command line. Be aware of ASLR. Use strace(1), ltrace(1), gdb(1) to understand the dynamic behavior of your or others software.
In some cases, generating parts of your C++ code (e.g. with ANTLR or SWIG) is worthwhile. Notice that Qt is doing so.
Consider using some cross-platform C++ frameworks such as Qt or POCO.
For some projects, writing your GCC plugin could be useful. See this draft report.
In short how do I consume Shared Object (.so) files from another project using Visual Studio 2019 compiling for Linux using WSL 2 ?
Don't use Visual Studio (I never used it myself, but according to rumors it is unfit for cross-compilation from Windows to Linux). Perhaps use Visual Studio Code (to which I prefer GNU emacs, but you might try gedit, geany, vim, kate etc...)
On Linux, all IDE for C++ programming would run a GCC or Clang compiler (and you'll need to understand what compilation command they are running for you). You could also be interested by the Clang static analyzer or by Frama-C++
Take inspiration from existing open source C++ projects on Linux
Look on github or gitlab - e.g. libonion, Qt, FLTK, fish, icecream) ... Read also Linux From Scratch.
above all, approach Linux programming with a fresh and open mindset.
Read about the Unix philosophy, it is relevant on Linux. And very different from Microsoft design ideas related to Windows.
I never used Windows (and I am coding since 1974), but my biased opinion is that WSL is targeted for Linux gurus who (sadly for them) have to use Windows. I believe WSL is not targeting Linux newbies.

step into system, CRTL functions with Eclipse in Linux

I'm a whiz with Visual C++, but Linux development is new to me. In Visual Studio, it's easy to trace into any code implemented by the C run time libraries. I just need to make sure the sources are installed and I can step right into any function I'd like -- malloc(), cout::operator<<(), whatever.
I'm trying to develop using Eclipse's C++ package. How can I step into C run time routines there? Since Linux is open-source, how do I step into operating system routines? Seems like it should e possible -- am I missing debug information, source code, or both? Something in my configuration?
I'm using Ubuntu 12.10 at the moment. I'm using g++. I believe I'm using the Eclipse build system as I never imported a makefile project; I just started with a simple "Hello World" project from the C++ project wizard in Eclipse.
After hacking at this a bit:
I've installed the libstdc++6-4.2-dbg package thinking it would be debug symbols for the libstdc library:
sudo apt-get install libstdc++6-4.2-dbg
I've also installed dpkg-dev, since the next step said I needed it:
sudo apt-get install dpkg-dev
I tried installing libc6 sources into a directory under my home:
apt-get source libc6
At this point, trying to step into printf() tells me that printf.c is missing. I can't step into malloc or strlen, which suggests that I don't understand how the C runtime libraries are factored in Linux. How are libc, glib, and libstdc++ different? Which packages do I need?
If I ask Eclipse to open the printf.c file I do have (at ~/eglibc-2.15/stdio-common/printf.c), it doesn't open the file (doesn't adjust the debugging window to show the source) and repaints the window that shows the error message about not being able to find the file. (Can't find a source file at "printf.c" Locate the file or edit the source lookup path to include its location.)
Whilst, as a Kernel developer on Linux, I do agree that using the individual tools separately will be a good thing to learn, and as such Basile's answer is usefuel.
However, the stepping into C runtime libraries should be equally possible with Eclipse. But just because the OS is open source doesn't mean that it supports you clambering around inside it willy nilly - in fact, you CAN NOT step into the OS itself from user-mode code. You nee KGDB (google it), and you definitely need a second computer to attach to the one being debugged, because when you step into the kernel, you will essentially lock up the machine, at the very least in the context you are stepping, but most likely also prevent other work from being done until you get back out from the kernel, so for example, if you step into open(), at some point the entire filesystem may well stop working altogether until you are back out of whatever lock you are holding. This wll certainly upset some software. Note that this is just an example of how things may work unexpectedly when debugging the kernel, not strictly "I've done this and it happened" - I have debugged kernels with debuggers several times, and you do have to be careful with what you do, and you certainly can not run the debugger on the same machine, as the machine STOPS when you are debugging.
Going back to the usermode, which you CAN debug via Eclipse, essentially all you need to do is install the source code for the runtime library you are interested in, and go... Same principle as on Windows with visual studio - except that nearly all software you ever run on a Linux system is available as source code. You may need to recompile some libraries with debugging symbols, and just like in Windows, you need to make sure the debugger knows how to find the source code. Everything else should be handled by the debugger in Eclipse. I spent about three years using Eclipse for both local and remote debugging, and in general, it works. There are quirks in places, but that's the case with almost any debugger.
Good luck.
First, you don't need Eclipse to develop software on Linux. You should better learn to do that with independent tools (command line) like emacs or gedit (as editor), git (version control), make (builder) which runs the gcc or g++ compiler (both gcc & g++ are part of GCC, the Gnu Compiler Collection).
really, you'll learn a lot more by not depending upon Eclipse; it may just hide you the real commands which are doing the job, and you should understand what they really are.
You want to pass the -g -Wall options to GCC. The -g option asks for debug information, and the -Wall options asks for almost all warnings. Improve your code till no warnings are given.
And the operating system is providing syscalls (which are operations provided by the kernel to applications; from the application's point of view, a syscall is atomic so you cannot step into it; however strace may show you all the syscalls done by some execution). If you wanted to step by step inside system libraries like libc you need the debugging variant of it (e.g. some libc6-dbg package). But there is usually no need to dive inside system libraries.
See http://advancedlinuxprogramming.com/
Then, you will use gdb to debug the binary program.
So, step by step instructions inside a terminal:
edit your source files with emacs or gedit
learn how to use GCC: for a single source C++ program compile it with g++ -Wall -g source.cc -o progbin and type ./progbin in your terminal to run it. Only when the program is debugged and satisfactory would you compile it with optimizations (by giving the -O or -O2 flag to gcc or g++)
Use gdb to debug a program (compiled with -g).
for a multi-file C++ program, consider learning how to use make
use a version control system like git
For beginners, I suggest to avoid Eclipse, because it just hides to you what is really happening underneath (Eclipse is simply running other tools like the above commands)
Software development under Linux requires a different mindset than under Windows: you really are using your own loose combination of independent tools, so better to learn a bit each of them.
NB. to step inside "system" functions like malloc (which is above syscalls like mmap) you need the debug variant of the libc package with aptitude install libc6-dbg, and you need to set LD_LIBRARY_PATH to /usr/lib/debug etc...

A simple "javac" style command-line C/C++ compiler for Windows 7

Over the last couple of months I practiced console programming with Java just with the help of JDK and a text editor of my choice (Notepad++). And I loved the simplicity as a program can be compiled from the command line plainly using javac and run using java.
Now, I'm looking for similar compiler for C/C++, such that I create a .c or .cpp file and compile it in the command prompt, and all it does is create a "native" executable that can be run directly from the command prompt. Thus, without any need of bloated IDE. The reason I'm looking for such simple compiler is because it is going to be used by high-school students so I'm advised to avoid any IDE as far as possible, so students can practice all the concepts of C/C++ languages without having to go for IDE. Which compiler can I use that does this job? also, I must work across all the versions Windows starting from Windows XP.
You can download MinGW which is basically GCC for windows.
Then you can simply gcc somefile.c to create an executable.
http://gcc.gnu.org/gcc is a multi platform c/c++ compiler
Visual Studio includes the ability to compile from the command line. Like others just said you can look at cygwin/MinGW. I would recommended using Code::Blocks or Dev-C++. I know you stated you do not want an IDE, but I would highly suggest a minimalist IDE like the ones I just suggested, or at least SciTE or Notepad++ to get some basic syntax highlighting with the ability to configure build tools if you want as well.
MinGW GCC is definitely the way go, but I would recomend the nuwen.net distro (http://nuwen.net/mingw.html). Haven't used it in a while (yay unix!), but if IIRC, it comes with everything ready to go after unpacking. The official distribution is ... very hard to get working.
A very simple solution woul be cygwin and MinGW, which provides an environment very similar to a UNIX shell. Then you can use the make utilities to compile your program.
You should certainly consider using MinGW GCC, but not by download from the MiNGW web page, unless you are some kind of masochist. Get the one packaged by Twilight Dragon Media at http://tdm-gcc.tdragon.net.
I too use VisualStudio on Windows from the command prompt and use VS Make files as well. That way, I can smb mount my source code from a different machine and perform compiles on several different platforms at once (e.g. Windows, Linux, Solarsi).

How do you try out small/simple C or C++ source codes?

It is very easy on Linux to fire-up vi and write a 100-200 lines of code, compile and see the results: ie. Trying small simple examples of C/C++ code.
On windows however, I like Visual Studio but to use it you have create a new solution then a project which then creates a new folder, generates very large PDB and caching files and a small example of 100-200 LOC becomes a 20Mb large project(?!) after compilation.
So the question is how do you write this kind of small codes on Windows? Possibly Cygwin or Dev-C++ (which is not active since 2004?).
You can compile from the command line using cl.exe. See the MSDN article How to: Compile a Native C++ Program from the Command Line for detailed instructions.
When you installed Visual Studio it created an entry in your programs named something like "Visual Studio Command Prompt" (maybe in a group "Visual Studio Tools").
Execute that Command Prompt (it sets up some environment variables needed for the command line compiler) and use cl, the command line compiler.
> cl /?
Copyright (C) Microsoft Corporation. All rights reserved.
C/C++ COMPILER OPTIONS
-OPTIMIZATION-
/O1 minimize space /O2 maximize speed
/Ob<n> inline expansion (default n=0) /Od disable optimizations (default)
/Og enable global optimization /Oi[-] enable intrinsic functions
...
Edit -- copy from another answer :)
Microsoft Documentation: VS2005, VS2008
For the simplest examples codepad may be an option.
I think there's nothing wrong with firing up Visual Studio for some testing. You can delete the 20MB afterwards ;)
However, you can also just invoke the command line compiler on Windows. Just start a Windows SDK console (or Visual Studio console) and you're there. And you can even use vi (need to install it first, of course).
I always use MinGW (GCC for windows) for such tasks.
MinGW is a good solution if you're not using anything Visual Studio specific. If you are, in the start menu with Visual Studio, there should be a script that starts a "commandline" for visual studio.
Also, please keep in mind, even if you aren't going to use Visual Studio, if you use MinGW, you're going to run into issues even with things you might not expect (like...if you decide to try the Apple Bonjour SDK, in which case you're going to get nasty link errors) because GCC and MSVC++ libraries don't always play nice.
And if you don't have access to your local computer, or you are to lazy to look for command line compiler you can paste your code to on-line compiler. There are several of them on the net you can try one of them here. It's free, no registration needed. It has some flaws but for quick code check it's just fine.
You can also use some other languages, C, C++, D, Haskell, Lua, OCaml, PHP, Perl, Python, Ruby, Scheme, Tcl to be precise.
Maybe I'm just a *nix geek, but I went on over to http://consoletelnet.sourceforge.net/gccwin32.html and got gcc for win32. I then headed over to http://unxutils.sourceforge.net/ and go these command line tools. (I renamed their find.exe to gfind.exe to avoid conflict with windows find). I then use gvim for win32 to write code and make/gcc to compile it. Now I only have to learn one environment.
For small bits of code I want to test, such as code that I'm going to include in an SO answer, I always use the command line. My tools on Windows are:
the MSYS tools, particularly their bash shell
the vim editor
the MinGW GCC compiler (Twilight Dragon branch)
I haven't used any of them, but MinGW and lcc-win32 seem to be pretty lightweight, and people seem to like them in news:comp.lang.c. MinGW is a port of GNU Compiler Collection for Windows and is free, lcc-win32 is free for non-commercial use.
You can do the exact same thing for Windows -- fire up VI and run the output through gcc.
Either get MingGW/MSys, Cygwin, or native ports of each app (gnu tools, vi)
Just because you're using Windows, doesn't mean you're forced to use Visual Studio.
I've got a Visual Studio test project with a main file I just overwrite every time I want to test something new. The couple of MB taken up by the .ncb file really really really doesn't matter. A bigger issues is the need to create a project. But I get around that by reusing an old test project.
For small/simple C or C++ source codes I use Ideone. It supports over 40 programming languages.
I usually edit my code within notepad++ then compile it with gcc under cygwin or msys.
I have used Dev-C++ post 2004, and it still works quite well.
I even used projects coded in Dev-C++ for my practicals that had to run on machines using Linux. Just changed the make files.
I also use Visual Studio; for quick testing and prototyping, I have a file scratch.c on my desktop, which I just load up and test things out in.
I don't see opening Visual Studio, clicking the new document icon, writing code, pressing F5 then just accepting the defaults for everything as being too much effort :)
The other alternative I have (which I don't use for C, but do for Haskell) is to PuTTY into a Linux box I have access to, and do everything on there.
I have a Test.sln which has a Test.vcproj which has a Test.cpp. The solution has a few handy configurations (for maximum C++ std conformance and others). I just paste code into/from the Test.cpp file and compile it that way.
I use cl.exe and nmake.exe from Visual C++ Express to compile small groups of .c and .cpp files. Rolling your own simple Makefile for nmake.exe is easy.

Brand new to C (how to compile?)

I would like to know what I should use to compile in C. I am brand new to programing in general and would greatly appreciate a comprehensive explanation of this process. I am on Windows Vista.
I heard about something called "djgpp" that is free and effective for windows.
For the answer to this question and many others you may have as you start out, try [this website][1] which has beginner tutorials. [Here's the page][2] on compilers and getting set up. An excerpt on what compilers you can use:
Windows/DOS
Code::Blocks and MINGW
Borland
DJGPP
Dev-C++ and Digital Mars
Windows Only
Microsoft Visual C++
nix
g++ is a C++ compiler that comes with most *nix distributions.
gcc is a C compiler that comes with most *nix distributions.
Macintosh
- XCode
Personally, I'd also recommend you strongly to read [The C Programming Language by Kernighan and Ritchie.][3] It will really help you understand both core principles of programming as well as C-specific details.
[1]: http://www.cprogramming.com/tutorial/c/lesson1.html
[2]: http://www.cprogramming.com/compilers.html
[3]: http://www.amazon.com/C-Programming-Language-2nd-Ed/dp/0131103709/ref=ed_oe_h
Visual Studio Express is a free version of Microsoft's leading IDE for Windows, and it includes a C development environment (editor, compiler, debugger).
Depends on which platform, but on Linux, Mac OS X and similar (including Cygwin), gcc is most common.
Say I have a script named myapp.c:
#import <stdio.h>
int main(){
printf("Hi!\n");
return 0;
}
To compile it from the command line using gcc is very simple, just run:
gcc myapp.c
..that outputs the binary as "a.out" (the default filename). To run the compiled app, just do..
./a.out
#which outputs: Hi!
Or put it somewhere within your $PATH (for example, /usr/bin or ~/bin/) and run a.out
You can specify the output filename with the -o flag:
gcc myapp.c -o myapp
./myapp
# output: Hi!
As for more complete guides, "Learn C for Cocoa" is a good, quick guide on the C language, and covers compiling. If you're interested in learning ObjC, the next part of the guide is also great.
On Windows, I would recommend looking into Visual Studio, specifically the Express editions (which are free).
There's no "Visual C", but the Visual C++ edition will compile C code (since C++ is an extension of C). There's also an option to "Compile as C code" somewhere in the build options.
Finally, The C Programming Language book covers, well, everything.
I would recommend using gcc and getting comfortable with the command line, if you plan on doing C for any extended amount of time. *nixes probably have it built in, and on Windows you can install MingW GCC and use it the same way.
I find this helpful because I feel like C was designed hand-in-hand with Unix, so it's better to stick with the command line than use an IDE off the bat. Most tutorials will probably assume you are using gcc. Also, if you're just learning C with gcc, you just type gcc -o outputfile sourcefile.c, whereas if you're starting with an IDE you'll have to figure out how the IDE works.
On the learning front, definitely pick up Kernighan and Ritchie's "The C Programming Language" as it's the definitive book on C. Rather slim volume, and easy to read.
Just a note, another helpful compiler flag for gcc is -g, as in gcc -o output -g -Wall sourcefile.c. -g enables debugging symbols, which will be useful in the future when you want to start debugging with gdb. This is another reason why you should start with gcc rather than an IDE, it makes it easier to move on to gdb.
You could always use MS Visual C++, but I think it's too complicated for a beginner. I personally recommend Notepad++ for editing as it doesn't do too much for you, but it formats the code nicely. Of course, once you become more experienced you might want to move on to a full-blown IDE with project management and such (or better yet, emacs ;)) but when you're just starting out simpler is probably better.
If you're on Unix (i.e. Linux, Mac OS X, FreeBSD, Windows under Cygwin, etc.) I recommend the GNU Compiler Collection (gcc). To compile a file hello.c, run this at the command prompt:
gcc hello.c
This compiles hello.c to the executable a.out. If you want a better-named executable, try this:
gcc -o hello hello.c
This compiles hello.c to the executable hello. Some more helpful flags:
gcc -Wall -Wextra -o hello hello.c
This turns on lots of helpful warnings that will probably help you catch bugs in your code.
On Windows, many people use Microsoft Visual C++, but I have no real experience with Windows. As I referenced above, you can install Cygwin, and then use the above steps, but many prefer pure-Windows compiling. Also, MinGW is a native-Windows port of gcc. It's all a matter of preference, though.
Welcome to programming!
You might want to start in the right track and install a copy of Linux in your box. Try Ubuntu cuz it's easy to install and learn
Next learn to use the console! It will help you so much in the next years.
Once ubuntu is installed you need to install C compiler and basic libraries just typing in the console
sudo apt-get install gcc build-essential
Then just create a C file and run
gcc myprogram.c -o myprogram
This will generate an exec file called yourprogram, run it using
./myprogram
Here are some useful links:
Ubuntu Home
Compiling/Running C in Ubuntu Forums
Linux Tutorial - READ THIS!
I would recommend downloading Microsoft Visual C++ Express Edition. http://www.microsoft.com/express/vc/ DJGPP will work as a compiler but does not provide an editor or coding environment. Compiling your code involves going to the command line. Some have suggested using gcc or MinGW but both of those programs behave similarly to DJGPP. Using a separate editor and compiling your code from the command line is not really the way that software is typically written in a Windows environment. Visual C++ will allow you to write and compile either C or C++ code.
Microsoft Visual C++ is a free of cost development environment which includes an editor and a compiler. Simply download the installer executable and run it to install. The installer will download the necessary software from the Internet and install it on your computer. The installer will download about 200MB of software so it may take up to an hour to download depending on your connection speed.
Once you have Visual C++ installed, start a new project and write your code. You can then compile and build it right from inside Visual C++ and see the output. Start by writing a simple command line program and then work up to more interesting programs.
For windows now you can use gcc as mentioned above but also MSC. You can get the compiler for free with the Visual Studio Express bundle for Visual C++ Express
Invoking the compiler from the command line is fine for small apps as you're getting used to the C syntax, but as you move along, you will find that entering the myriad options gets tedious, and you will want to use a build tool. make is a basic but powerful way of compiling your projects easily. Editing makefiles by hand can be tedious, and will get in the way of your learning, so I'd suggest skipping right over that and go for the autotools toolchain.
Ok Josiah, first of all, be sure to have a C compiler installed on your system. On Linux, Mac, there's a compiler installed just out of the box, called the GCC compiler. And on Windows, you need to instal one, I recommend you to instal an IDE (like devshed) that includes an editor, compiler, debugger... all that you need to develop a C/C++ application.
In order to compile over Linux/Mac, first of all you'll need a source file (a text file with your code) and save it with a .c or .cpp extension. Then open your terminal and type:
gcc <fileName.c>
You should replace <fileName.c> with your filename (the full path, or first go to the files location and just type the file name).
This will create a .o file (and object file) in the same path of your source file, this is the executable file for your computer!
On windows, if you already installed an IDE, is easier, you just click the compile button and there ya go!
I hope this to be helpful to you