Is there a way to #include "library.a" or it's .so or it's .o inside a cpp or cc file?
I would like to do this because I am using this file inside another file.
#include is for C++ code.
.a, .so and .o files are not C++ code.
It's more likely that you want to #include a C++ header file (typically ending in .h or .hpp), and link an object file.
You can do this in the Visual C++ compiler, using #pragma comment(lib, "libname")- and the similarity is somewhat dubious. However you will have to discover any alternatives for your own favourite compiler.
#include is used for telling the compiler about the functions from a library which you will call in your code, that is to say it includes C++ code. Generally this takes the form of header files which have the function declarations in them.
.a and .so and .o files are compliled code which can be linked into your compiled code using the linker.
Edit: there's an introduction about compiling and linking here
The #include statement basically just includes other source code into the current file. That being said, a static library is not source code and cannot be included in this manner. Static (and shared) libraries are instead linked into the project after all the compiling is done.
What you have to do is to include a file containing prototypes to the functions you are going to be using. This way the compiler knows it is there and the linker will sort out the rest.
For more information on how to create and link static/shared libraries, check out this page.
It simply doesn't make sense.
You include the library's code at compile time by linking it.
Normally there's a header file for the library you can include.
Related
This may be a naive question since I'm a c++ beginner.
In a typical cpp project, we have cpp files and their header files. So based on my understanding, usually cpp files have definitions, while respective header files have declarations. During compiling, we have to include very cpp files in the project since header files do not specify the location of the definition of the declared object. Finally, linker links declaration and definition. This allows the communication between different translation units.
However, when it comes to std:: library, we do include header files but never compile its cpp files. For example, we write #include in main.cpp, but when compiling, the command does not have anything like g++ vector.cpp main.cpp ...
I even tried to search cpp files in the std library folder in my system, but all I can find is various header files.
I do know the definition and declaration can be included in a single header file, I don't think this is how std:: is wrote. Otherwise, it will result in multiple copies of definition when included in different cpp files.
Please help me to understand how this works? Can I do something like this with my own project?
When it comes to templated libraries, the source code must be found in the header files (.h or .hpp), so it is not exceptional to find header-only libraries.
If part of the code is not templated and its implementation is made in source files (.cpp), usually the code is precompiled and supplied as .lib (.a/.so) files to be linked to.
As regards open source APIs, if there are .cpp files they are delivered as well and you have to compile/precompile them by yourself.
Yes, there is compiled code in the standard library. You don't have to compile it because the library comes already compiled with your compiler. And you don't explicitly link to it, because the compiler knows about it. So you don't see it mentioned anywhere when you do normal builds. If you set your compiler to some form of verbose mode you can see that the name of the standard library gets passed to the linker, which pulls the code it needs from the library.
When you build an executable in C++, when including header files
For example,
#include <iostream>
Does the Preprocessor find the library iostream library and the function used or is the object code for the library functions injected at Linking?
All #include does is load in the definitions of the library functions and doesn't have anything to do with the binary instance of library itself.
Think of the header files as blueprints on how the library works, but does not provide the actual components that the program needs. It's just so the compiler can understand how the library works.
In order for that to come into play you must also link in the associated library files. This is done with different arguments at the linking stage.
I have a program in C++ and compiled the program into a static library .a file. Now I want to use this library in MATLAB. Seemingly all remaining steps to use it in MATLAB is to give to MATLAB this .a library and the header files using loadlibrary(libname,hfile) command.
The problem is that my program consists of about 200 .h files located in different folders under the main source folder. How can I give all of these header files to MATLAB loadlibrary? I just started to integrating all of the headers into one header file; but is there a better, easier way to this?
This will require some work on your part.
First option is to add additional header files in the loadlibrary command
loadlibrary('mylib','mylib.h','addheader','header2')
You'll need to have a list of all your additional header files.
Alternatively, you can explore the prototype file option instead of ehader file.
See this link for more information.
I was looking at the source code for ncmpcpp, and I saw this.
#include <mpd/client.h>
Inside that file are functions which are used by ncmpcpp. But those are just the headers. Don't the cpp files have to exist somewhere as well? I couldn't find them in the same directory. Where are they?
Also, when something that's included is surrounded by < and >, how do I know where to look?
If it's a third-party library, most likely the source code won't be included. Nor is it needed. All symbols declared in the headers (which are meant to be used) should be exported in the .lib file which was probably shipped with the headers.
Unless you have templates, which could be inline.
You only need the cpp files, or, more generally, the implementation files, if you want to compile the code yourself. Which you don't. You can use the module having just headers and binaries.
Of course, the example of open-source projects comes to mind, where all files are generally included, but if it's a commercial product, why release the source code? What's keeping any competitors from just copying it and resell it under a new name?
There's no standard rule that tells where to look for headers that are delimited by <> or "", but the general consensus is that <> are to be used for system headers (like string or iostream) and "" for own headers (myclass.h). It just tells the compiler where to look first.
The source files aren't needed if there is a library (static or dynamically linked) with it that the compiler can link to, these are generally .a or .lib files (though very rarely you might need a .def file).
For search paths, see this for MSVC and this for GCC.
I'm building a socket program in Visual Studio 2003 .NET
I #include <winsock2.h> header file but also noticed that I had to link in the WS2_32.lib to fix the unresolved winsock function errors.
In other homework projects, I just added a header file and used it's functions - without adding the corresponding library.
How is this so?
Are some standard header file libraries already pre-linked in Visual Studio or something else?
Thank You!
The socket functions are actually implemented in ws2_32.dll. In order for the linker to be able to find them, you need to add the ws2_32.lib import library to your project. Note that the import library does not contain the actual code for the functions, but only information about where to find the actual functions (in ws2_32.dll).
You don't mention which other header files you're referring to, but if it's something like <string.h> then that is already in the MSVC runtime library; if it's something like <windows.h> then those functions are provided by import libraries such as kernel32.lib, user32.lib, and gdi32.lib. Those libraries are probably already included in your linker settings.
By default, Visual Studio includes the most commonly used Win32 .lib files, e.g. kernel32.lib, user32.lib, advapi32.lib etc. For more esoteric libraries, you need to add the .lib files yourself.
By default Visual Studio will link against the standard library, so if you are including a header that is part of that then you don't need to explicitly add the library. This is true for things like stdio.h, iostream and stdlib.h.
There are also some header files such as those used by the Standard Template Library (someone will be along in a minute to say that it's actually just called the 'Standard Library', but most books I've ever read, and Microsoft's docs too refer to the STL) such as <vector> and <list> which define all their code as templates that get expanded into the full functions by the compiler so that they don't need to link in a library.
A slight aside: there's also a mechanism for automatically linking against a library. Just add:
#pragma comment(lib, "ws2_32.lib")
somewhere in your code. Boost uses this technique so that it links against the correct build of the library depending on your compiler settinsg.
This has nothing to do with Visual Studio; this is how compilation of C/C++ works.
All a header does is declare or define symbols. Functions, variables, typedefs, classes, etc.
A header can say:
int SomeFunction();
This is a function declaration. In order for you to compile code that uses SomeFunction, you must declare that SomeFunction exists. And this declaration must be made before your code that uses it.
These declarations are typically in header files.
However, a declaration is also a promise. A function definition is the actual C/C++ source code that makes the function work. A declaration says, "at some later point, you will be able to find the definition of this." This is a promise you are making to the compiler and linker.
You cannot successfully link C/C++ code unless all declarations in use have a definition. Some of these definitions come from your own compiled code, but some of them come from external libraries. External libraries have header files that provide the declaration of C/C++ functions, types, etc. But they also have library files (in VC++, these use the .lib extension) that provide the definition of those functions.
If you use declarations from a header, without linking to the library files that provide the definition of those symbols, you get a linker error.
Note that header files can contain definitions as well; much of the C++ standard library, and must of Boost is defined solely by header files. So there are no libraries to include. The library's documentation should tell you whether there is a .lib to link against or not.