Tool Native Library Generator (C/C++ to B4A)

logo.png

Native Library Generator 4.20 (2018-11-11)
Written in B4J, this program allows you to write or import C/C++ code and have all the functions compiled into a native (.so) library, which in turn is included in a ready-to-use B4A Java lib. When using already existing code, if present, the main() function will be automatically ignored. If you're familiar with C/C++ but not with JNI or NDK, this is the right tool for you! No JAVA required.

Requirements:

Open Source:
This project is open source, under the GNU General Public License. Enjoy! :)
https://gitlab.com/brunowonder/NativeLibraryGenerator-CommunityEdition

Price (pay what you want):
If you wish to buy me a beer (or even dinner!) just follow the link below.
https://www.paypal.me/ninjadynamics
0317.png
0317.png
0317.png

How does it work:
upload_2017-10-27_15-57-1-png.61061


Video Demos:

Updates:
[Version 4.21] [2018-11-12] [CRITICAL]
QuickSource mode fixed
For background music add "nlg.mp3" to the NLG folder ;)

[Version 4.20] [2018-11-11] [NEW FEATURES]
Now supporting multiple source files
Select which files should be exposed to B4A
Requires NDK r18b (please update)
Bug fixes

[Version 4.01] [2017-05-07] [NEW FEATURES]
The 'cpu-features' library can now be used
Custom Makefiles may now be used
Custom Java wrappers may now be used
Bug fixes

[Version 4.00] [2017-05-07] [NEW FEATURES]
Complete visual overhaul
Specifying additional include paths is now possible
Specifying additional prebuilt libraries is now possible
Specifying the target ABI's is now possible
Compatible ABI's are now auto-detected if possible
Handling
multiple projects is now possible

[Version 3.0] [2017-05-07] [NEW FEATURES]
Returning arrays is now possible.
B4A logging with printf() is now possible.


[Version 2.9] [2016-08-12] [NEW FEATURES]
Small UI redesign and bug fixes.
Code clean-up.

Little bit more user-friendly.
Custom makefiles.

[Version 2.8] [2016-07-12] [NEW FEATURES]
Now supporting local #include files.
Now supporting external project locations.
New video demo.
Minor bug fixes.

[Version 2.7] [2016-03-14] [CRITICAL]
Major bug fixes.

[Version 2.6] [2016-01-31] [IMPORTANT]
Important bug fixes.
MazeSolver 1.5 Included as example source code.

[Version 2.5] [2016-01-31] [NEW FEATURES]
Now supporting comment transcription from C++ to B4A.
New splash screen.
Minor bug fixes.

[Version 2.4] [2016-01-27] [CRITICAL]
Critical bug fix regarding string (char*) support.
Cool new intro/exit screens added.


[Version 2.3] [2016-01-25] [NEW FEATURES]
Now supporting Strings (char*), both passing and returning.

[Version 2.2] [2016-01-20] [IMPORTANT]
Now supporting simple C/C++ typedef struct.
Now supporting C/C++ inline functions.

Minor bug fixes.

[Version 2.1] [
2016-01-12] [CRITICAL]
Included example (NinjaCore) source code updated. Several bug fixes.

[Version 2.1] [2016-01-12] [IMPORTANT]
Now supporting C++ headers, such as <vector> or <algorithm>.

[Version 2.0] [2016-01-10] [CRITICAL]
Major issue corrected. Please download Native Library Generator again.

Included Example:

In order to showcase this software's capabilities, I decided to include my MazeSolver project.

A. Download link:

B. Instructions (QuickSource Mode):
In QuickSouce mode all you have to do is write some code and hit the Generate Library button.

1. Before anything else, please make sure you have installed the following required software:
B4A, Simple Library Compiler and Android NDK


2. Download the .jar file into a system-writable folder (NOT Program Files).

3. Run NLG and provide the necessary software paths, as seen below.
upload_2018-11-12_11-56-16.png


4. Compile the example by clicking the big blue button.
upload_2018-11-12_11-56-37.png



5. Test the library you just compiled with this B4A project:
https://www.b4x.com/android/forum/t...orithm-w-path-optimization.61998/#post-391230

6. As you can see, it works!
upload_2018-11-12_12-14-11.png

C. LibFastMath Example code (Copy/Paste to NLG):
B4X:
//LibFastMath 1.00 - Native Library Example

//FastSin() and FastCos() by Allen Chou
//http://allenchou.net/2014/02/game-math-faster-sine-cosine-with-polynomial-curves

//FastSqrt() adapted from Quake 3's Fast Inverse Square Root algorithm
//https://en.wikipedia.org/wiki/Fast_inverse_square_root

#define PI         (3.1415926535f)
#define HALF_PI    (0.5f * PI)
#define TWO_PI     (2.0f * PI)
#define TWO_PI_INV (1.0f / TWO_PI)

float FastSin(float x);
float FastCos(float x);
float FastSqrt(float x);

inline float Hill(float x)
{
  const float a0 = 1.0f;
  const float a2 = 2.0f / PI - 12.0f / (PI * PI);
  const float a3 = 16.0f / (PI * PI * PI) - 4.0f / (PI * PI);
  const float xx = x * x;
  const float xxx = xx * x;

  return a0 + a2 * xx + a3 * xxx;
}

float FastSin(float x)
{
  // wrap x within [0, TWO_PI)
  const float a = x * TWO_PI_INV;
  x -= static_cast<int>(a) * TWO_PI;
  if (x < 0.0f)
    x += TWO_PI;

  // 4 pieces of hills
  if (x < HALF_PI)
    return Hill(HALF_PI - x);
  else if (x < PI)
    return Hill(x - HALF_PI);
  else if (x < 3.0f * HALF_PI)
    return -Hill(3.0f * HALF_PI - x);
  else
    return -Hill(x - 3.0f * HALF_PI);
}

float FastCos(float x)
{
  return FastSin(x + HALF_PI);
}

float FastSqrt(float x)
{
    //This is the method used in Quake3
    const float xhalf = 0.5f*x;

    //Get bits for floating value
    union
    {
        float x;
        int i;
    } u;
    u.x = x;

    //Give initial guess y0
    u.i = 0x5f3759df - (u.i >> 1);

    //Newton step, repeating increases accuracy
    return (x*u.x*(1.5f - xhalf*u.x*u.x));
}

D. How does it work:
Initially, I created this program in order to speed-up the process of writing JNI C/C++ code. Following B4A's RAD philosophy, it allows me to write native code in Visual Studio and compile it into a fully-working B4A lib within a single click.
E. Internal Functions:
If you wish any of your native function not to be exposed in the B4A library, you may do so by either changing their name from foo() into privateFoo() or simply adding a "::ignore" the function's comments.
B4X:
//This function will not be visible in B4A because its name starts with "private".
int privateSum(int a, int b)
{
    return a + b;
}

//This function will not be visible in B4A because this comment contains the keyword "::ignore".
int multiplication(int a, int b)
{
    return a * b;
}

//This function will be exposed in B4A.
int result(int a, int b)
{
    return privateSum(a, b) + multiplication(a, b);
}
F. Current Limitations:
As happy as I am to share this work with the B4x community, let's not forget that this is just my own internal tool, a never ending work-in-progress personal project.
Hence, it does not yet support several features such as passing Arrays into the native layer.
G. Reminder:
Please test your C/C++ code before trying to build a B4A library, as some kinds of syntax errors may crash the application.
 

Attachments

  • upload_2018-11-12_11-53-30.png
    upload_2018-11-12_11-53-30.png
    88.2 KB · Views: 425
Last edited:

H&Bit

New Member
Licensed User
Dear wonder
I am a contributor. I saw your blog in b4x. I think that your achievement is incredible and great. Recently, I downloaded open source in your link but
it did not compile that. It presented 'Error occurred on line: 1030'.-> (Dim returnSpider =0 As int) Please help me to solve this problem.
Also Could you please give me two files of native_library_generator_XXX.jar and xml files? Please help me. Best regards
 

Attachments

  • Error.png
    Error.png
    84.1 KB · Views: 239

monic

Active Member
Licensed User
Longtime User
Doesn't the error box state the error(s), can you provide a sample and what doers the log box show a error before compile?
 
Last edited:

wonder

Expert
Licensed User
Longtime User
Dear wonder
I am a contributor. I saw your blog in b4x. I think that your achievement is incredible and great. Recently, I downloaded open source in your link but
it did not compile that. It presented 'Error occurred on line: 1030'.-> (Dim returnSpider =0 As int) Please help me to solve this problem.
Also Could you please give me two files of native_library_generator_XXX.jar and xml files? Please help me. Best regards

This bug has been corrected more than a month ago, but it seems that I forgot to post about it.
Sorry about that.

The fixed version can be obtained from the GitLab repository.
I will update the .jar file on the first post until tomorrow.
 

wonder

Expert
Licensed User
Longtime User
NLG 4 wishlist:
- Support for multiple source files
- Better handling of header files
- Import project settings from Visual Studio / Code::Blocks
- Transparent makefiles
- Selection for C or C++ compilation
- Unification and documentation of '// @NLG: ... ' commands
- UI improvements

Wish me luck! :)
 

inakigarm

Well-Known Member
Licensed User
Longtime User
NLG 4 wishlist:
- Support for multiple source files
- Better handling of header files
- Import project settings from Visual Studio / Code::Blocks
- Transparent makefiles
- Selection for C or C++ compilation
- Unification and documentation of '// @NLG: ... ' commands
- UI improvements

Wish me luck! :)
Luck ! ;-)
 

canalrun

Well-Known Member
Licensed User
Longtime User
Thank you Wonder. This is really neat.

I wonder if the following would be technically possible:

If I read correctly your tool takes C/C++ code and compiles it to a .so library, then also creates a B4A wrapper library so that the .so can be used in B4A.

Is it possible to start with a third-party .so library and have the tool produce the B4A wrapper to use the .so in B4A?

This would require that the .so library file contains all the required information needed to produce a wrapper – I don't know enough about .so libraries to know if they contain all the information needed.

Thanks,
Barry.
 

wonder

Expert
Licensed User
Longtime User
If I read correctly your tool takes C/C++ code and compiles it to a .so library, then also creates a B4A wrapper library so that the .so can be used in B4A.
Correct.

Is it possible to start with a third-party .so library and have the tool produce the B4A wrapper to use the .so in B4A?
Yes, I already managed to get Python working inside B4A... ;) but the current NLG version doesn't support it yet. Work in progress... ^_^

This would require that the .so library file contains all the required information needed to produce a wrapper – I don't know enough about .so libraries to know if they contain all the information needed.
You have to know the target architecture (at least) and know your way around the source code (at least a little bit).
 
Last edited:

wonder

Expert
Licensed User
Longtime User
NLG 4.00 is out! :)
- Complete visual overhaul
- Specifying additional include paths is now possible
- Specifying additional prebuilt libraries is now possible
- Specifying the target ABI's is now possible
- Compatible ABI's are now auto-detected if possible
- Handling multiple projects is now possible
 

wonder

Expert
Licensed User
Longtime User
[Version 4.01] [2017-05-07] [NEW FEATURES]
The 'cpu-features' library can now be used
Custom Makefiles may now be used
Custom Java wrappers may now be used
Bug fixes

First post updated. :)
 

wonder

Expert
Licensed User
Longtime User
Hi!

I've been working on this project a little bit, improving things here and there.

Experimental features:
- Projects with multiple source files
- When existing, the config file your_project\nlg\config.txt has now priority over your_nlg_path\projects.txt

If you're interested in this build, you can get it here:
https://gitlab.com/brunowonder/NativeLibraryGenerator-CommunityEdition

upload_2018-10-10_0-16-2.png


EDIT:
Known issues:
- When utilizing multiple native libraries in your B4A project, if the target ABIs of one of them doesn't match the other ones, your app will crash!

I concluded that IS NOT an NLG issue:
How to fix: https://www.b4x.com/android/forum/t...e-native-libraries-problem.99207/#post-624540
 
Last edited:

wonder

Expert
Licensed User
Longtime User
New Release!!! :cool:;):D:p

[Version 4.20] [2018-11-11] [NEW FEATURES]
Now supporting multiple source files
Select which files should be exposed to B4A
Requires NDK r18b (please update)
Bug fixes

I'll try to record some new video tutorials soon.
 
Top