winsock2 undefined reference to `__imp_WSAStartup' [duplicate] - c++

In Visual C++, one may link to a library in the code itself by doing #pragma comment (lib, "libname.lib"). Is something similar possible in g++?

The Boost Config library has some support for autolinking, using the relevant compiler-specific code for the particular compiler. However, the docs note that the GCC toolchain doesn't support autolinking:
Auto-Linking
Most Windows compilers and linkers
have so-called “auto-linking support,”
which eliminates the second challenge.
Special code in Boost header files
detects your compiler options and uses
that information to encode the name of
the correct library into your object
files; the linker selects the library
with that name from the directories
you've told it to search.
The GCC toolchains (Cygwin and MinGW)
are notable exceptions; GCC users
should refer to the linking
instructions for Unix variant OSes for
the appropriate command-line options
to use.

Related

Where is the standard library?

I have searched Google but haven't found quite a direct answer to my queries.
I have been reading C++ Primer and I'm still quite new to the language, but despite how good the book is it discusses the use of the standard library but doesn't really describe where it is or where it comes from (it hasn't yet anyway). So, where is the standard library? Where are the header files that let me access it? When I downloaded CodeBlocks, did the STL come with it? Or does it automatically come with my OS?
Somewhat related, but what exactly is MinGW that came with Cobeblocks? Here it says
MinGW is a C/C++ compiler suite which allows you to create Windows executables without dependency on such DLLs
So at the most basic level is it just a collection of "things" needed to let me make C++ programs?
Apologies for the quite basic question.
"When I downloaded CodeBlocks, did the STL come with it?"
Despite it's not called the STL, but the C++ standard library, it comes with your c++ compiler implementation (and optionally packaged with the CodeBlocks IDE).
You have to differentiate IDE and compiler toolchain. CodeBlocks (the Integrated Development Environment) can be configured to use a number of different compiler toolchains (e.g. Clang or MSVC).
"Or does it automatically come with my OS?"
No, usually not. Especially not for Windows OS
"So, where is the standard library? Where are the header files that let me access it?"
They come with the compiler toolchain you're currently using for your CodeBlocks project.
Supposed this is the MinGW GCC toolchain and it's installed in the default directory, you'll find the libraries under a directory like (that's what I have)
C:\MinGW\lib\gcc\mingw32\4.8.1
and the header files at
C:\MinGW\lib\gcc\mingw32\4.8.1\include\c++
"So at the most basic level is it just a collection of "things" needed to let me make C++ programs?"
It's the Minimalist GNU toolchain for Windows. It usually comes along with the GCC (GNU C/C++ compiler toolchain), plus the MSYS minimalist GNU tools environment (including GNU make, shell, etc.).
When you have installed a C++ implementation you'll have something which implements everything necessary to use C++ source files and turn them into something running. How that is done exactly depends on the specific C++ implementation. Most often, there is a compiler which processes individual source file and translates them into object files which are then combined by a linker to produce an actual running program. That is by no means required and, e.g., cling directly interprets C++ code without compiling.
All this is just to clarify that there is no one way how C++ is implemented although the majority of contemporary C++ implementations follow the approach of compiler/linker and provide libraries as a collection of files with declarations and library files providing implementations of these declarations.
Where the C++ standard library is located and where its declarations are to be found entirely depends on the C++ implementations. Oddly, all C++ implementations I have encountered so far except cling do use a compiler and all these compilers support a -E option (although it is spelled /E for MSVC++) which preprocesses a C++ file. The typically quite large output shows locations of included files pointing at the location of the declarations. That is, something like this executed on a command line yields a file with the information about the locations:
compiler -E input.cpp > input.ii
How the compiler compiler is actually named entirely depends on the C++ implementation and is something like g++, clang++, etc. The file input.cpp is supposed to contain a suitable include directive for one of the standard C++ library headers, e.g.
#include <iostream>
Searching in the output input.ii should reveal the location of this header. Of course, it is possible that the declarations are made available by the compiler without actually including a file but just making declarations visible. There used to be a compiler like this (TenDRA) but I'm not aware of any contemporary compiler doing this (with modules being considered for standardization these may come back in the future, though).
Where the actual library file with the objects implementing the various declarations is located is an entirely different question and locating these tends to be a bit more involved.
The C++ implementation is probably installed somehow when installing CodeBlocks. I think it is just one package. On systems with a package management system like dpkg on some Linuxes it would be quite reasonable to just have the IDE have a dependency on the compiler (e.g., gcc for CodeBlocks) and have the compiler have a dependency on the standard C++ library (libstdc++ for gcc) and have the package management system sort out how things are installed.
There are several implementations of the C++ standard library. Some of the more popular ones are libstdc++, which comes packaged with GCC, libc++, which can be used with Clang, or Visual Studio's implementation by Microsoft. They use a licensed version of Dinkumware's implementation. MinGW contains a port of GCC. CodeBlocks, an IDE, allows you to choose a setup which comes packaged with a version of MinGW, or one without. Either way, you can still set up the IDE to use a different compiler if you choose. Part of the standard library implementation will also be header files, not just binaries, because a lot of it is template code (which can only be implemented in header files.)
I recommend you read the documentation for the respective technologies because they contain a lot of information, more than a tutorial or book would:
libstdc++ faq
MinGW faq
MSDN

Build boost library with specific name

I am trying to compile my program, which uses boost library, with MSVC 2013 and I get link error:
Cannot open input file boost_iostreams-vc120-1.57.lib
I already built boost successfully a few times with many combinations of options (for example "bjam toolset=msvc --build-type=complete"), but I don't have boost_iostreams-vc120-1.57.lib in my stage directory. I have there libraries with names libboost_iostreams.lib, libboost_iostreams-vc-120-mt-1_57.lib and others. Also I don't want to change any settings in MSVC, because my project was generated by CMake.
So the question is: How do I build library boost_iostreams-vc120-1.57.lib with Windows?
Thanks for answers and sorry for my english
-mt suffix means build with multithreading support that is always on for MSVC. Single threaded CRT was dropped in VS 2008 or even 2005, so both your code and boost will be multithreading aware anyway. If you don't want to change your build config you can simply remove "-mt" suffix from libraries names. But since boost has MSVC autolink support (#pragma comment (lib, "...") in header files) it may be better to make an exception for MSVC and not to link to boost libraries manually at all.

Intel (windows) c++ compiler and changing its library implementation to gcc. Is it possible?

Not sure if this is the right place to ask but here goes
From a page on the Intel website, it states:
The Intel C++ Compiler for Windows uses the Microsoft Visual C++ header files, libraries and linker. Microsoft controls the header files that define the namespace. Contact Microsoft's technical support in reference to Microsoft's conformance to the C++ standard on this issue... link
Is there a guide by Intel (or otherwise) to change the libraries from the ones governed by visual studio to ones provided by gcc (Also on my windows machine). the reason I want to do this is to make use of some of the new C++11 features that are not supported in versions of visual studio (as is generally the case)
If this is not possible because my current knowledge of the above is not correct, can somebody explain to me why not.
Thanks.
This is not a practical possibility.
The intel compiler (icl) will do nothing but moan if it cannot find VC++ binaries on the PATH, so you know it needs the VC++ toolchain at least.
Then to see what you are up against, as far as using the gcc headers is concerned, you would do the following:
Make icl suppress its own predefined macros.
Make it use gcc's predefined macros.
Make it suppress its standard include search.
Make it use gcc's standard include search.
None of this is hard, and when you have done it all and attempted to build your HelloWorld.cpp, the errors
will show you that the gcc headers are replete with builtin keywords of the gcc compiler that are
unknown to icl: __builtin_va_list, __attribute__, __cdecl__, __nothrow__ and so on.
You might attempt to successfully delete or redefine all of these by way of preprocessor macros.
Or you might give up, and I would urge the latter.

Is it possible to use libraries build under VC++ in cygwin gcc build?

I have libraries which are build using VC++. I want to use the same libraries in a program and build in cygwin gcc compiler. Is this scenario will work?
Since C++ doesn't have a standardized ABI, and since Visual C++ and g++ in particular almost certainly have different ABIs, I don't think you can do this. Your only real option is to restrict yourself to C, not C++.
Edit: If all of the libraries that you're using are straight C (extern "C", in C++ terms), then you ought to be able to share libraries between compilers. (Otherwise, your Cygwin and MinGW apps couldn't call the Windows API.) You'll need to be careful to match up calling conventions. For example, the Windows API uses __stdcall instead of the standard C calling convention. You can tell GCC which calling convention to use with function attributes. Google, and your library's header files, should have more information on what calling convention to use.
Probably not
There is mingw a port of GCC (and lots of gnu tools) to windows which uses standard windows libs - it's pretty much a drop in replacement for cygwin and should be easier to link to gcc.

Help on linking in gcc

In Microsoft visual c++ compiler, you can specify linker options using
#pragma comment(lib, "MSVCRT") //links with the MVCRT library
see this page
I find this feature very useful because linker errors are common and i want to just place all the linker options in my source code instead of specifying them to the compiler.
question: Is there a way to do this in gcc (or dev-cpp or codeblocks ide)?
Thanks in advance.
GCC doesn't support this because to link correctly, the order in which you link your objects matters.
See also my answer and others in the question "#pragma comment(lib, “xxx.lib”) equivalent under Linux?"
In short, GCC does not support specifying libraries to link in the source code.
If your IDE handles the compilation and linking process, you can probably add references in your IDE and have it worry about passing the correct options to gcc for each unit.
Given that link options and library names vary very much from system to system, I am quite glad to have them separated from my source code files and thus can keep the source code system independent.
Then the build system can decide how to build on what system. Much cleaner approach overall, I'd say.