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.
I'm taking a MOOC course on data structures and algorithm. I would like to use c++, and I need to set my compiler options to the following
g++ -pipe -O2 -std=c++14 -lm
I'm currently using MS Visual Studio 2017 on Windows. Is it even possible? Do I need to do custom build? The following is the paragraph taken from the MOOC mentions about Windows users having to use Cygwin, but I have no clue what that means. Can anybody shed some light on a feasible way to do this?
Your solution will be compiled as follows. We recommend that when
testing your solution locally, you use the same compiler flags for
compiling. This will increase the chances that your program behaves in
the same way on your machine and on the testing machine (note that a
buggy program may behave differently when compiled by different
compilers, or even by the same compiler with different flags).
C++ (g++ 5.2.1). File extensions: .cc, .cpp. Flags
g++ -pipe -O2 -std=c++14 -lm
If your C/C++ compiler does not recognize the "-std=c++14" flag, try
replacing it with "-std=c++11" or "-std=c++0x" flag or compiling
without this flag at all (all starter solutions can be compiled
without it). On Linux and MacOS, you probably have the required
compiler. On Windows, you may use your favorite compiler or install an
environment such as cygwin.
As they said you should choose cygwin (https://cygwin.com/install.html).
These would be the flags for msvc: /std:c++14 /O2 /Im
You can set compiler options for each project in its Property Pages
dialog box. In the left pane, select Configuration Properties, C/C++
and then choose the compiler option category.
For the categories lookup: https://learn.microsoft.com/de-de/cpp/build/reference/compiler-options-listed-by-category
Say I have a open source C::B C++ (non-C++11, perfectly compatible with the 1998 ISO standard) project I've downloaded which is using MinGW/GCC (TDM-1 4.7.1 or 4.7.2 - doesn't work with newest version),; can I port the source files from it to Visual Studio 2010 and be able to make it work without massive code rewriting? Or there are certain cases in which it won't be possible? Or it depends on various things?
EDIT:
The code relies on additional external utilities and libraries such as:
Lua
SDL 2.0 + SDL Image 2.0
OpenGL
The most General and correct response is: It depends on various things.
What kind of project are you refering to? Is it wxWidget, QT4, GTK+, OpenGL?
How much do you use c++11?
Assuming that we are talking about a simple Console Application the easiest way to verify whether you can migrate to MSVC2010 is to switch compiler inside Code::Blocks project.
Select Project->Build Option... and under Selected compiler choose Microsoft Visual Studio C++ 2010. Afterwards try to recompile. The warning and errors will show you how easy will be the porting.
Of course you have to install Code::Blocks with MSVC2010 too.
EDIT: The OpenGL library is supported by MSVC2010 and libsdl has VC development libraries. However, things seem to be more complex with Lua. My guess is that you might start a substantial porting work here.
If the project was written using portable C++98 code you shouldn't have too much trouble. First, I would check you can compile in GCC with -std=c++98 -pedantic flags and fix any warnings to ensure you are not relying on any GCC extensions.
It also depends on the portability of any required libraries.
Try it!
If your code is standard-compliant and does not rely on any GCC extensions or GCC-specific libraries, then you should be fine out-of-the-box.
Note though that different compilers support C++ in different ways; for example, even Visual Studio 2013 has only passing C++11 support so if your program is a C++11 program with things like ranged-for and initializer lists in it then, depending on the version you're using, it's just not going to work without those pieces of code being rewritten to look more like C++03.
I actually made a mistake:
C::B indeed told me there were multiple errors in the compilation attempt with the MSVC2010 compiler, because the code included many Unix-only libraries, too intricately so to be easily avoided. Thus, I'm thinking of either making MinGW/GCC work in Visual Studio itself or sticking with C::B.
(Continues here: POSIX Headers (from MinGW project) in Visual Studio 2013)
I recently bought a laptop with Windows 8.1 on it and I'm having trouble using the NetBeans C++ IDE on it. When I build, this is the window that comes up:
And I'm assuming these are my options:
I've tried using C:\MinGW\bin\gcc.exe in the field with the red asterisk but that has done nothing for me.
All help appreciated.
I use C++ in Netbeans 8.0* using gcc, g++ in Cygwin. Cygwin was installed first, and then Netbeans C++.
Everything was done automatically for me.
You are missing the C++ compiler. It would probably help to fill in that line (see the red asterisk).
You would probably have
C:\MINGW\bin\g++.exe.
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