How to include a "custom" library in Code::Blocks? (Windows 7) - c++

Ok, I've never did this before and I'm having problems to understand how it works (Especially the "include" part). Here's my code:
#include <joypp1.02/app.h>
#include <joypp1.02/objects.h>
void event_plot()
{
if (App::event_type() == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
App::shut_down();
}
if (App::event_type() == ALLEGRO_EVENT_KEY_DOWN)
{
if (App::event().keyboard.keycode == ALLEGRO_KEY_ESCAPE)
{
App::shut_down();
}
if (App::event().keyboard.keycode == ALLEGRO_KEY_ENTER)
{
cout << "Whatever! ";
}
}
}
void visual_plot()
{
static Image x("sample.png");
x.draw();
}
int main(int argc, char **argv)
{
App app(800, 600);
app.set_background_color(200, 0, 0);
app.add_event_scene(Scene("Event Plot", event_plot));
app.add_visual_scene(Scene("Visual Plot", visual_plot));
app.run();
return 0;
}
joypp1.02 is the dynamic-link library I've compiled, and it's in its own "output" folder (Together with the import file):
joypp1.02 has only two headers: app.h e objects.h. How do I import them? The library is in the linker (I want to keep it in the output folder of the library, because It's in constant development. BUT, I want to compile it only as a DLL, so I have a separate project that uses the DLL).
And the folder with the joypp1.02.dll is in the Search Directories of the compiler.
And if I try to include like this:
#include "joypp1.02.h"
I get:
fatal error: joypp1.02.h: No such file or directory
So, I'm lost. How to include a "custom" library in Code::Blocks in Windows? How do I know the include name? Why it's a header like "joypp1.02.h" if it's a .a file?
Disclaimer: I've used the word "custom" to emphasize that it was written and compiled by me (So, probably more problems than just importing some "official" and "pro" one).

Well, I've solved it. I was missing the point of the actual function of a dynamic-link library. I was forgetting to include the header files (And I've remembered from other libraries that, indeed, I include their .h files apart from the .a or .lib import library files, and the binary .dll(s) (I thought they were inside the import library file or something, and that only the import library and .dll were required)).
So you, possible reader from the future, in face of the same problem: Hear me! You must make the header files of your library visible to the compiler (Include in the "search directories", in the compiler tab in Code::Blocks), and at the same time, link the import library .a or .lib file, AND, if no search directory is specified under the linker tab, have the .dll binary in the folder of your application.
If by any means, you're still lost, read this:
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_(errors)#Q:_What_do_I_need_to_know_when_using_3rd_party_libs.3F

Related

Trying to create Library in C++. Test project can't find implementation of declared constructor?

I'm learning C++, but hit a plateau trying to create a library. Using Visual Studio 2013, I get this error:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Polynomial::Polynomial(void)" (??0Polynomial##QAE#XZ) referenced in function _main A:\Medier\Desktop\PolyLib\Test\Test.obj Test
The testproject is currently cut down to this:
#include "A:\Medier\Desktop\PolyLib\PolyLib\PolyLib.h"
using namespace std;
void main()
{
Polynomial poly;
}
The library header file, PolyLib.h, looks like this:
using namespace std;
class Polynomial {
private:
double* Coefficients; //Array of coefficients - Pointer type.
int Degree; //The highest exponent of the polynomial
public:
Polynomial(); //Default constructor - Creates the trivial polynomial 0.
Polynomial(int degree, double coefficients[]); //Constructor for specific polynomials
Finally there's a CPP file in the library, PolyLib.cpp, providing implementations for the header file. Looks like this:
#include "PolyLib.h"
using namespace std;
Polynomial::Polynomial() {
Degree = 0;
Coefficients = new double[Degree + 1];
Coefficients[0] = 0;
}
Polynomial::Polynomial(int degree, double coefficients[]) : Degree(degree), Coefficients(coefficients) {
int i = 0;
}
I understand that my test project looks to the PolyLib.h file to see what stuff is available. And I understand the error says it can't find the actual implementation of the empty constructor for Polynomial. As such I conclude that the PolyLib.cpp file is not being used.
Can anyone tell me where I go from here?
It is possible to build all of your .cpps into a single project, which is most common for beginners. However, from the question tags and wording, I'm assuming you want to create and link a static lib, which can also be done fairly simply.
I just constructed an example of a properly set up static lib + exe linking in one VS2013 project: Dropbox link, for the purposes of explaining this question.
The basics are that you need two projects: A lib project (outputs a static .lib file) and an exe project (outputs an .exe file). The .exe project contains a main() function, and is able to include headers and link to the .lib.
The points of interest in MyExe are that I added $(SolutionDir)\LibExample\Inc to the include path in order to include my header (header.h), and I added $(SolutionDir)\$(Configuration) to the lib path for the libraries. You can find these by right clicking the project, go to properties, and it's all under "VC++ Directories".
A great tip about determining the path needed to find your lib is to simply build the lib. It actually tells you in the "Output->Build" text spew where the file it just built is located. This is the path that you need. $(SolutionDir)\$(Configuration) is the "macro" shorthand of this path. I recommend using the macros so you can move your project directory to another path or computer easily.
I also added LibExample.lib; to "additional dependencies" in the linker\input settings. This is the key step for being able to link the lib. Not doing this will result in an unresolved external symbol, just like you're seeing.
I also set a dependency between MyExe and LibExample (you right click the project, Build Dependencies->Project Dependencies). This is important so you can rebuild the entire solution without worry of linking to the older version of the lib. With the dependency in place, it will always build the lib first, then the exe.
If you are trying to build Polylib.h and Polylib.cpp into a binary such as Polylib.lib then you need to have a separate project that has these source files and builds a static library. Then you need your project to link this Polylib library so that it can link the code into your binary. If you just want to have one project and only build one binary then you need to move the Polylib.h and Polylib.cpp source into your project and build it all together. Another thing is that using absolute paths for including headers is a bad idea. You want to include headers using relative pathing and then use the project settings to tell it where the headers are located. For example if its local to the project then use #include "header". If its not local to the project then use #include <libraryfolder/header> and then tell the project where the library folder is in the "include directories setting".

Is there a way to search for functions among a large folder of libraries (.a and .so files) in UBUNTU?

I have a project in eclipse, using a library and I can import its include files (include directories) to my project without any problem. But I don't know which libraries to include (I know a library path with zillions of libraries) to use some specific functions.
In Ubuntu, is there a tool to search for functions through a list of libraries? There is a tool called "nm", which lists all exported libraries, structures variables etc. of a PARTICULAR library, if there is no "search for function" tool, I will need to write a program that uses "nm" to search and find functions.
Adding in project's proprieties link of your libraries and the headers, you can use the autocompletion, where is the problem?
Supposing you are using Foo library, you have to link it:
C/C++Build->Settings->C++ Linker->Libraries
Put Foo (-lFoo) in the list.
C/C++Build->Settings->C++ Compiler->Includes
Put *.h Foo path.
So in your program.. main.cpp
#include <Foo/A.h>
int main() {
A a;
a.
}
When you click "dot" you see all A method available. Supposing that A has print method
int main() {
A a;
a.print();
}
You can go to the definition of method print() by: Ctrl+RightClick

How to build library written by C++ and use in iOS

I now have to porting C++ code to iOS, trying to build static library by original C++ code and load the library on iOS. Because the original code is heavy, I start a small test to verify my steps could work or not.
First I need to build library (.a), which prints some string. I compile the following code and generate a library(.a) file
//talk.h
...
#include <iostream>
class Talk {
Talk();
void printHello();
void printWord(char*);
};
//talk.cpp
#include "talk.h"
using namespace std;
void Talk::printHello() {
cout << "Hello World";
}
void Talk::printWord(char* word) {
cout << "Hello" << word;
}
The second step I try to do is open a new project for iOS app and then set link to the library file, also include corresponding "talk.h" header file.
However, some errors happen on the header file even though I build library successfully.
The errors indicate that
"iostream" file not found
"Unknown type name 'class'; did you mean 'Class'?
any other errors...
I have try to rename controller.m to controller.mm, but it not fixes the problem
How to import the header file written in C++ for using library on iOS?
Thanks
A rough outline:
In Xcode (starting with an either one of the template iOS Application projects (or an existing one):
Create a new static library target: File -> New -> Target.... Select Framework Or Library, and then Cocoa Touch Static Library
Add library source code: Drag library source code into Xcode project. In the dialog that appears, select the build target created above.
Add project dependancy to library: Select Project in Project Navigation, the the iOS build target from Targets. Select Build Phases tab, then under Target Dependancies in the window, click on the + sign. A sheet opens (choose items to add) and the library target should be at thee top of the list.
Include Library in iOS Target: Under Link Binary with Libraries, click +. The library (a .a file) should be at the top of the list.
Link with libc++: As the step above. Select libc++ from the list.
Enable Objective-C++ compilation for any source code file that needs to include the library headers by changing the extension from .m to .mm
Build the iOS application target.
Xcode will have taken care of setting everything else up for you, including compiler flags and header search paths.
For the 1st part C++ compiles well with Xcode
I have written a simple tutorial
http://tutorialsios.blogspot.com/2013/09/c-beginers-code-for-objective-c.html
These code were written using Xcode and compiled in terminal
#include "functionOverLoading.h"
int main(int argc, const char * argv[]) {
..
..
return 0;
}
$g++ functionOverLoading.cpp –o functionOverLoading
$ ./functionOverLoading
You need to add the C++ Standard Library.
To do this, find Other Linker Flags in your project and add -lstdc++.

Lesson needed on how to work with files in different projects in C++

I have been able to work in the same project for sometime now, writing and successfully running c++ code. However, I discovered that I am still missing some essentials on how to export my .h files to another project and successfully use them in there.
I created a second project, project B to test the classes I have in project A.
visual c++: #include files from other projects in the same solution
I added the path of the header file in Project A into the Additional Include Directories(C\C++>general and Linker>general) section in the project configuration of Project B. I tried following the tutorials on this page http://msdn.microsoft.com/en-us/library/ms235636.aspx but I still end up with the error below
** LINK : fatal error LNK1104: cannot open file 'C:\Users\LaC\Projects\OSGB\Debug\OSGB.lib**
I would appreciate any help in understanding exactly how this is done so that in future, when I encounter this problem, I can know how to troubleshoot.
The code below is all I am working with.
IN PROJECT A
=============
//Utility.h
class Utility
{
private:
protected:
public:
Utility(void);
~Utility(void);
double square_root (const double);
};
//Utility.cpp
#include "StdAfx.h"
#include "Utility.h"
Utility::Utility(void)
{
//do nothing for now
}
Utility::~Utility(void)
{
//do nothing for now
}
double Utility::square_root (const double)
{
return 0;
}
IN PROJECT B
===============
#include "gtest/gtest.h"
#include "Utility.h"
TEST (SquareRootTest, PositiveNos) {
Utility u;
EXPECT_EQ (50.3321, u.square_root (2533.310224));
}
There are two (general) ways to include files into your project:
Make them a part of your project (adding them from the solution explorer) OR
Import them as a library (static or dynamic linking)
If you make them part of your project, then you have to add the header and the source files in order for the project to compile correctly. However, that's usually not what you want to do, as it defeats the purpose of having external libraries.
The second case is to use the external libraries, which requires that you:
Include the header files which are exported by the library in your C++ properties.
For static linking: you also have to include the *.lib file (the output of building the library) in the Linker properties.
OR
For dynamic linking: see tutorial.
So remember: there are two parts to building a C++ project- compiling and linking.
Compiler Errors:
If you get an error whose code starts with C* (e.g. C1083) and is related to problems header with the files, then check the Properties-> C/C++ -> General -> Additional Include Directories.
Linker Errors:
If you get an error whose code starts with LNK*, then check
Properties -> Linker -> General -> Additional Library Directories (make sure that this points to where the *.lib file is located)
AND
Properties -> Linker -> Input -> Additional Dependencies (make sure that the *.lib file is added here).
If you're dynamically linking, then check that you're correctly referencing the DLL.
So in your case, you have to determine if you're linking statically or dynamically and then make the appropriate references. So ware you getting those header files from a dynamically library or a static library?
When the linker emits unresolved external symbol for a symbol that lives in another library (DLL or shared library), this indicates that you need to link your app to that other library's .lib file. That is most likely what's happening here.
For more information see:
(MSDN) Walkthrough: Creating and Using a Dynamic Link Library (C++)

How do I install a c++ library so I can use it?

I have this library called BASS which is an audio library which I'm going to use to record with the microphone. I have all the files needed to use it, but I don't know how to install the library. I tried taking the example files and putting them in the same directory as the bass.h file. But I got a bunch of errors saying there are function calls that doesn't exist.
So my question is, how do I install it to be able to use it?
Installing a C++ library means specifying to interested software (eg. a compiler) the location of two kinds of files: headers (typical extensions *.h or .hpp) and compiled objects (.dll or *.lib for instance).
The headers will contain the declarations exposed to the developer by the library authors, and your program will #include them in its source code, the dll will contain the compiled code which will be or linked together and used by your program, and they will be found by the linker (or loaded dynamically, but this is another step).
So you need to
Put the header files in a location which your compiler is aware of (typically IDE allows to set so-called include directories, otherwise you specify a flag like -I<path-to-headers> when invoking the compiler)
Put the dll files in a location which your linker is aware of (surely your IDE will allow that, otherwise you speficy a flag like -L<path-to-libraries> -l<name-of-libraries>
Last but not least, since I see that BASS library is a commercial product, probably they will have made available some installation instructions?
Run this command in a terminal or console.
cpp -v
Notice at the end of the output, you'll see a line like this:
#include<...> search starts here:
There will be a list of directories below that line.
Move the package folder to one of those directories.
Then try importing the module with <>.
See the code below code and don not forget to put bass.dll in the directory of your exe file and include the file bass.lib with your project and don not forget also to include the path to bass.h and bass.lib in the default include and lib path of your project.
#include <iostream>
#include "bass.h"
using namespace std;
int main(int argc, const char **argv)
{
if (!BASS_Init(-1, 44100, 0, NULL ,NULL))
{
cout<<"Can't initialize device";
return -1;
}
int stream = BASS_StreamCreateFile(false, "D:\\mypro\\Trans_Langs\\germ\\quran_amma\\Translations\\Sound_aya\\Sora1\\Hafs\\basfar\\a7.mp3", 0L, 0L, 0);
if (stream != 0)
{
// play the stream channel
BASS_ChannelPlay(stream, false);
}
else
{
// error creating the stream
cout<<"Stream error: {0}", BASS_ErrorGetCode();
}
getchar();
BASS_StreamFree(stream);
// free BASS
BASS_Free();
return 0;
}
If there are files named configure, Makefile or install you can try running them in that order. After that, any program that wants to link with this library must use a command like this:
c++ <your_program.cpp> -l<library_name> -L<path_where_library_is_installed>
The library path is usually the original library folder itself, unless you explicitly change it or the library itself puts its files in global locations like /usr/local or something like that.