How to include multiple classes in a single library include? - c++

I'm making a library that consists of multiple classes.
Here are the files used:
mylib.cpp
mylib_global.h //Qt requirement for shared lib
mylib.h //This is what i'd like to import
oneclass.cpp //The classes below provide the functionality
oneclass.h
twoclass.cpp
twoclass.h
I would like to achieve the following:
#include "mylib.h"
int main(int argc, char *argv[])
{
OneClass oneClass;
TwoClass twoClass;
}
So, i'm just importing the mylib.h in some other application header and because of it the OneClass and TwoClass are available there.
Can this be achieved?
Also, please comment if this is a conceptually wrong way to implement libraries, and if so, why?

Yes, that can be achieved.
Simply include all public definitions that your libraries provide into the libraries main header file.
In your example make mylib.h should look like as follows:
#include "oneclass.h"
#include "twoclass.h"

Related

c++ Library with nested includes

So I'm trying to create my own library in C++ and use it in another project.
So far it works with example code, but I have to include other libraries in my own library. So the problem is, that when I include the header files off my library,
the include paths in the header files are messed up.
A simple solution would be to add the search directories, but I don't think,
thats how its supposed to be resolved.
Sample Code - Library header file:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int test();
The source file:
#include "sample.h"
int test() { return 20; }
Now the project in which I want to include the sample
#include <sample.h>
int main() { int a = test(); }
The problem is, that the include copies the code from sample.h directly into the main.cpp and the search directories for the other includes from sample.h are no longer defined
A simple solution would be to add the search directories, but I don't think, thats how its supposed to be resolved.
This is certainly the easiest solution since it requires no modifications to the code, and is usually an acceptable thing to do - however obviously it means the project can call the functions from glew.h and glfw3.h
The only alternative is to ensure the headers are not included by the library header, but instead by the source.
IE:
Library Header:
int test();
Library Source:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "sample.h"
int test() { return 20; }
And the project's source file left unchanged.
This requires that the types defined in glew.h and glfw3.h are not part of the public interface exposed by your library.
Eg, if your library had a function like:
GLFWwindow* window = openWindow(...);
You would need to change it to:
Library header:
struct WindowHandle;
WindowHandle* openWindow(...);
Library source:
struct WindowHandle{
GLFWwindow* window;
};
WindowHandle* openWindow(...){
WindowHandle* result;
//... do stuff...
result->window = //whatever;
return result;
}
This approach requires changing the library code, but has the advantage that the users of the library can't directly call the things the library depends on (glew and glfw in this case). This is particularly beneficial if you want to support multiple platforms, you could have a source file for opening windows via glfw, and another using direct x. The library's public interface would not need to be changed to support both backends.
If you want to know more about this approach try searching for "Opaque data types"

Header only library inline not working

I am struggling with my library design. I want to create a library to be used in my future projects (header only for now...)
I have this file structure:
C:\Libs\MYLIB
- Tools.hpp
- Tools.cpp
The code looks like this:
Tools.hpp
#pragma once
class Tools
{
public:
Tools();
~Tools();
};
Tools.cpp
#include "Tools.hpp"
inline Tools::Tools()
{
}
inline Tools::~Tools()
{
}
And then there is is another project, in a totally different folder including this file:
#include <MYLIB/Tools.hpp>
int main()
{
Tools t;
return 0;
}
But I always get this error:
undefined reference to Tools::Tools()'
undefined reference toTools::~Tools()'
When I create a .hpp file only with inline implementation inside my class, like this:
#pragma once
class Tools
{
public:
Tools(){};
~Tools(){};
void DoSomething(){};
};
it works (so my include paths are correct), but I don't want to bloat my .hpp file (I want to use doxygen later on, and keep my declaration from implementation).
I know using inline can be ignored by the compiler, I guess this is what happens here?! So what is the best way to create a private header only library
My specs:
Win 10 with CodeLite
MinGW (g++)
Another question:
Should this line in Tools.cpp
#include "Tools.hpp"
better be like this:
#include <MYLIB/Tools.hpp>
You can use an ad-hoc static lib, which is convenient for libraries that are small and/or change often and thus don't provide much benefit compared to the overhead of versioning/compiling/distributing separately. The idea is that you separate headers/sources as normal, but you just #include the .cpp file in one translation unit. Be aware that this technique has its benefits, but also limits!

Is this C header written properly?

I am learning (trying) how to write my own C/C++ headers, and get functions out of the body of my code. For this I wrote nyanlib.h, and nyan.cpp
Ignoring that its a trivial program, please tell me if the header is written correctly, or if I am making serious mistakes? The code works though.
Here is nyan.cpp:
#include <iostream>
#include <unistd.h>
#include "nyanlib.h"
using namespace std;
int main()
{
while( true )
{
print_nyan(); //function from nyanlib
sleep(1);
}
return 0;
}
and Here is nyanlib.h
Also, Would nyanlib.h be a shared library or a static library?
What you have as nyanlib.h should be renamed to nyanlib.cpp. Then nyanlib.h would contain only the following:
#ifndef NYANLIB_H
#define NYANLIB_H
void print_nyan();
#endif
You can then compile the library as an object file:
g++ -c nyanlib.cpp
This gives you nyanlib.o. So now your main file contains just a prototype of print_nyan() from the header file so it knows how to call it.
Then you compile the main program:
g++ -c nyan.cpp
g++ -o nyan nyan.o nyanlib.o
Yes and no. While it is a valid header, you are using it a bit strangely. Most of the time, a header is used as a way to expose methods and variables related to the class and not to perform functionality.
For your case if you want to make it "better", I would bring the implementation of the print_nyan() function into the cpp file, and just leave a prototype of the function in the header.
And I would say that it is neither a static nor a shared library since it has a main. If you wanted to make it a library, you should make a separate header and source file that defines the print_nyan() function, make that your library, and use that library in your main function.

Make an #include <header.h> only visible for its own library

Is it possible to hide an
#include <header.h> //from library B
from projects using the library A?
I need to keep the include there because otherwise my library A won't compile, but as that header is from other library B that shouldn't be visible for the main project, the main program tries to find that header.h and compilation fails.
You could put it inside an #ifdef and define the symbol only when compiling library A:
#ifdef INCLUDE_FROM_LIBRARY_B
#include <header.h> //from library B
#endif //INCLUDE_FROM_LIBRARY_B
Update: but probably the best option is #Robinson's suggestion above: don't include it in any header file, only in .cpp files when needed.

Importing Functions from a different C file

I want to import functions from another file in Microsoft Visual C++ 6.0. How can i do this? I have tried with this as follows:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#import <functions.cpp>
where functions.cpp is the file name from where I want to import the functions. But this gives an error: F:\CC++\Term Project\Dos Plotter\Functiom Plotter.cpp(6) : fatal error C1083: Cannot open type library file: 'Functions.cpp': No such file or directory
How can I solve this problem?
The #import directive is used with type libraries, often COM or .Net, not C++ source files. For complete details, see the MSDN page.
In order to include C++ functions from another file, you typically want to use the #include directive (details). This includes the code from the given file during compilation. Most often, you should include a header containing the function prototypes; it is possible to include code files, but not commonly needed or always safe.
To do this, you should provide two files, a header and a source file, for your functions.
The header will read something like:
#pragma once
void Function(int arg);
and the source:
#include "functions.hpp"
void Function(int arg) { ++arg; }
To use this in another file, you do:
#include "functions.hpp"
void OtherFunction()
{
Function(2);
}
You should also note that a header should typically be included only once. The MSVC-standard method of guaranteeing this is to add #pragma once to the beginning.
Edit: and to address the specific error you've posted, which applies to both #import and #include, the file you're attempting to include must be somewhere within the compiler's search path. In Visual Studio, you should add the necessary path to the project includes (this varies by version, but is typically under project properties -> compiler).
1) Did you mean functions.hpp? C/cpp files should not be #included unless you know very well what you're doing.
2) Add the location of the file to the custom include path in the project properties, or use the include "foo" format instead of include <foo>
3) Import is undefined in C. You need to separate prototypes and implementations, include-guard the prototypes file, and #include the prototypes file.
having the file functions.cpp on the same dir, use include "functions.cpp" instead
Name the file imported-function.hpp, and make sure that it is in the same dir. Or, you could link it to
Linux: /home/uname/appfolder/imported-function.hpp
Windows: C:\Username\uname\appfolder\imported-function.hpp
ChromeOS: /home/chronos/u-4e4342ea6b3b92244e7d4753922f0dc7125f4a1d/MyFiles/appfolder/imported-function.hpp