How do I compile my own C++ library for Android? - c++

I have written and tested a library in C++. The code even works in my Android application if I add the source files directly. While I do have experience compiling static and dynamic libraries for common operating systems, I have zero experience compiling for a mobile system like this. I've done some research, and I'm still a bit lost as to exactly how to approach this. For example, I am unsure of whether to build a makefile for use with ndk-build or to just invoke one of the Android's compilers directly.
I did see this question, but it does not quite match my situation. I just want to run build and have it spit out libfoo.a (I'd like to produce libfoo.so as well, but libfoo.a is of greater interest to me right now.) The example in that question's winning answer implied that it would build the library as one step/module for building the final application. I tried doing it that way just to see, but I had no luck.
Can anyone please guide me in this endeavor?
CLARIFICATION -- I do not want to build the library and immediately pipe it into an application. I want a .a or .so file that I can link against in multiple future Android applications.

Create a dummy java file with empty code and make sure there is a android_main function in your C++ code. Build using ndk-build. the resulting apk will make your library an application.
See the samples from the android-ndk-r5/samples directory, see the sample native-bitmap to get some idea.

If I understand it correctly that a shared library is not acceptable and you want to be static (but why is that so important?), probably the easiest way to do so is to simply supply source code that can be added to a project.
Ultimately there is nothing special about the ndk build system other than it knowing the right commands to issue to build the necessary files for the assortment of curent android architectures. If you really want to do something different, you can log the operation of the android build system in creating a shared library, and then write your own Makefile that performs the analogous steps to create a static library. HOWEVER, you will have to update your Makefile any time the underlying assumptions or target collections change in a new android version.

Related

How to create a portable executable with allegro 4.4.2

So I am creating a C++ program using the allegro version 4.4.2 library and I need to be able to produce an executable package which I can submit to a course instructor and have run on his computer without issues. I am developing the program using DevC++. If I am understanding this correctly so far, I need to link to a static library for allegro and then the executable should include the library info and thus the library itself is not necessary to have included in the executable package. I have also seen some recommendations regarding third party tools that produce executable packages, but these seem to be more general (not necessarily allegro compatible?) and I am hoping to avoid downloading more third party software.
I have tried linking to the static library liballegro-4.4.2-monolith-static-mt.a but for some reason when I do this I get a whole slew of undefined reference compile time errors. I have read that it is necessary to have #define ALLEGRO_STATICLINK included in the project, I have this statement at the top of my main source file but I am not sure if I am meant to define this somewhere in the project options instead?
Any guidance or link to resources which will help me solve my problem would be extremely appreciated! I am just getting into the meat of programming with C++ beginning to learn object oriented programming methods, I still occasionally struggle with the basics so thank you in advance for your help!
Backstory (if interested/relevant?): I am taking a Game AI programming course for which we need to create some example programs to demonstrate AI algorithms. The course specifies Java but I am most familiar with C++ and the course instructor says this is fine but I must be able to submit an executable to him which he can just run on his computer without issues. To best achieve what the course asks I feel the allegro library (which I already have installed) will be of great help, mostly for drawing graphics to the screen and such.
So I just ended up using a dynamically linked version of my program and including the appropriate .dll files which turned out to be a lot less work than I thought it would be.
Basically I just had to ensure the allegro-4.4.2-md.dll from the bin folder was included in the folder with the executable. I then sent off the package (the .exe compiled file and the needed supporting files such as bitmaps sound files etc) to a few friends and had them try to run it. They then gave me the warnings from their computers along the lines of "Test.exe cannot be run ****.dll cannot be found".
I searched the exact names of the dll files they were receiving warnings for in the folder with my game and voila it now seems to execute on whatever PC it is sent to.

How should/could/must I handle the dll that my C++ projects depend on?

I'm lost here and I have no clue how to proceed. This is not a question about how to make my program work, this is a question about how to stop wasting my time.
My programming environment is Visual Studio 2013 on windows, in C++.
I use 3 libraries extensively, namely: boost (using dynamic linking), OpenCV, and Qt.
During the development, I have configured VS to look at those 3 libraries by default for include and .lib. I have also added the 3 folders containing all the dlls to my PATH environment variable.
It works, but it is sometime painful, let me explain you when.
First hassle: Anytime I have a LNK error telling me I miss a function, it is usually on OpenCV since it has only one include file referencing all the functions. I have to look at OpenCV's source code to see what module this function belongs to and to know what I must link my program to.
Second Hassle: When comes the time to deploy my application, I have to ship it with all the relevant dlls. To know which one I need, I open dependency walker and try to forget nothing, I have then to test it on a different computer because 102% of the time I have missed a couple, and then I have to configure my Installer generator to include all those one by one.
Third Hassle: To ease a little bit the process of configuring a new development machine, I have recently switched to NuGet. It is great, I add boost with a couple of clicks to any project. But now my boost DLLs are everywhere, I have one folder per boost library, and since there are dozens of those I can't even add them all at once to my PATH now, so I have to move them manually to the appropriate folder, and that is really not what I want to do with my not-so-precious-but-who-are-you-to-judge time
I have looked around and couldn't find any good practice regarding this issue, maybe because they are too obvious, or too specific to a particular setup.
How do you do? How would you do if you were me?
We put all our external dependencies in version control along with the code. This ensures that all code can build "out of the box" on any of our development machines and also ensures that for any given version of the code, we know exactly which dependencies is has.
The best way to check for missing dependencies is how have a good automated test suite, if you've got comprehensive converge then if your tests pass you must have deployed the required libraries.
In terms of linking to the appropriate libraries, unfortunately, that just sounds like an issue with the structure of OpenCV (I'm not familiar with OpenCV). I tend to use dumpbin under Windows and nm under Linux to easily grep for symbols when I get link errors with an unfamiliar library.

How build harfbuzz without dependencies

I want build latest harfbuzz-ng library on Windows 7. But in build system by default I must go through long quest to gain: ragel, pkg-config, gtkdocize and other stuff. Even in the end if I get all what need for build system I get errors (sorry cannot say which concrete errors), last time I tried to build this library 2 month ago). Maybe on Linux system it is easy to get and build all this stuff but on Windows always something doesn't want to be compiled. Or the problem is that I don't find instructions which guide me on Windows, only Linux.
What I want to get is simple instructions how build only harfbuzz-ng with freetype dependency (and maybe add ICU) by MinGW compiler on Windows.
Thanks you very much.
For anyone reading this, have a look at harfbuzz.cc in the project which makes you able to use harfbuzz without any build system, just include it in one of your sources, no build system is required, define HB_NO_MT or even HB_TINY (which brings a minimal harfbuzz) if you don't use harfbuzz in multithread mode.
I don't know i this is the answer you are looking for. I am trying to work with Harfbuzz as well and did this through several attempts which did not work out for various reasons.
One (old) example you could use as a starter is: HarfBuzz static lib. The .lib-file generated works with UCDN and just needs this file plus a source for the Freetype-functions to provide the necessary FT_Face-parameter (i did this by compiling another .lib via the freetype-sourcecode (freetype.org/download.html).
But I am unsure if this will work out for the MinGW-compiler (you don't use VC++ as IDE do you??).
You may find of interest the MSYS2 build system. It is essentially a linux-style packaging system built on top of Windows(TM). You can download and install binary packages with simple commands (including automatic dependency solving). If you want to download a source package you can do that as well. http://msys2.github.io

How to convert a cmake project into a Visual Studio equivalent?

The situation is the following: I have the source code of one programm (lets call it programA) (written in C and C++), as well as the CMakeLists.txt and CTestConfig.cmake files. I already installed programA using CMake's graphical user interface and, as it is obvious, it worked. It created the .exe file (I'm working on Windows 7 OS).
The problem is that, right now, I've been asked to edit the program (and so, I must be able to edit the code and degugging it as changes are made). I also need to compile it but not in .exe anymore but in .dll so I can add it to a website we have.
I've read in forums that CMake can compile programA into a .dll if I need to, but as I would need to make some changes I consider that CMake debugging is not as useful and easy as using entirely VS. From the little I know from CMake language, the CMakeLists.txt is mainly used to check the OS of the user as well as adding some libraries in case they are not found.
I have to admit I have no idea in programming CMake directives, as I have been working with ASP.NET, C, C++ and C# mostly. Then, my idea is to try to work only in visual studio 2010 instead of using cmake as well, so once I have the program 'adapted' to VS and can be compiled just using VS, I'm ready to start my job. So the question I have is how can I perform the same task CMake did just using Visual Studio (Is there any way of implementing CMake directives in VS?), can VS compile by receiving as an argument something similar to that CMake.txt file (though it needs to be translated into another language)?
To skip the use of CMake I tried to copy the source code into a new project in VS. However as it does not use the CMake directives when compiling, it gives several errors, most of them related to the fact that some headers.h can't be found (cause they might be in a subfolder). And there are so many subfolders to add the paths to the predefined directories of search that it would take ages.
I'm sorry I can't be more precise in my explanation. I'm good at programming little projects on my own, but it's the first time I have to work on other's programm. Please don't hesitate to ask if anything was not properly understood
I would appreciate a lot any suggestion / advice /guidance you can give.
To make a dll, use add_library command and the SHARED keyword
add_library(mylib SHARED ${files})
this is easy with CMake, don't go back in visual that will be harder at the end
The Good News
Fortunately, cmake can generate VS Projects automaticaly for you (this tutorial s specific for OpenTissue, but Steps 1 to 3 should be the same for you).
The [not so] Bad News
Depending on the complexity of the project, VS Projects automaticaly generated by cmake can get pretty nasty, to the point of illegibility. It will, for example, hard link any library dependencies using the specific paths of your machine, so the project will most certainly not be portable across setups. In any case, that's the intended bahavior, because the primary idea of supporting this generator is simply making it work, thus allowing users to easily compile projects using MSVC, so there's not much you can do here. Nonetheless, it should work in your machine and will certainly be a great starting point for you, just create a project yourself from scratch copying the relevant parts out of the automatic generated version.

Can I use Win32 FreeType without the .dll file?

I'm teaching myself OpenGL and I'm implementing ttf text rendering using FreeType 2. I downloaded the library from
http://gnuwin32.sourceforge.net/packages/freetype.htm
and after a couple of minor issues I got it running properly. The thing that's bothering me is that I have to place a copy of freetype6.dll in the directory with my executable in order for the thing to run. I generally try to avoid a bunch of unnecessary dll files floating around. I'm sort of new to windows programming, but from what I understand most libraries can be built to run fully from a lib rather than requiring a dll at runtime. Looking through the documentation from FT is making my brain melt, so I thought I would ask here to see if there were any devs that have worked with FT before and if so, do they know how to build the library such that no dll is required at runtime.
Thank you in advance for any advice or support.
Check out this link. See the section Optional: Installing FreeType (by compiling it yourself)
Follow the instructions and you'll be good to go.
you can generate static lib by getting source code of it... then you won't need dll...and i think freetype2's source is available...
It is something DLL generic. All your DLLs should be in PATH similarly to LD_LIBRARY_PATH. Also under Windows (unlike Unix) the current directory is always in the PATH. So you just need to set your PATH variable to point to location of this dll.
Now, for Unix... you probably just have this library installed by default like hundreds of other useful libraries that are not present under Windows by default.
So... No unless you link statically you should use DLL somehow. And my suggestion - use dll.