I am trying to create an application in JAVA that could communicate with an existing C++ application via message queue in LINUX.
I have tried JNI using SWIG but it seems SWIG doesn't support C++ as for C it is working fine but for C++ it is throwing some error which on analysis over web shows its incapability for C++.
factorial.cpp
#include "factorial.h"
int fact(int n) {
if(n == 0) return 1;
return n * fact(n-1);
}
factorial.h
extern "C" {
int fact(int n);
}
factorial.i
%module factorial
%{
#include "factorial.h"
%}
int fact(int n);
Main.java
public class Main {
static
{
System.loadLibrary("factorial");
}
public static void main(String[] args) {
int result = factorial.fact(5);
System.out.println(result);
}
}
Then command for generating .java files is
1: swig -c++ -java factorial.i
and then commands for compilation of factorial.cpp and factorial_wrap.cxx files are:
1: g++ -c -fpic factorial.cpp
2: g++ -c -fpic factorial_wrap.cxx -I /usr/java/jdk1.8.0_25/include -I /usr/java/jdk1.8.0_25/include/linux
then command for making libfactorial.so
1: ld -G factorial.o factorial_wrap.o -o libfactorial.so
then to compile Main.java:
1: javac Main.java
2: java Main
then i am facing this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: factorialJNI.fact(I)I
at factorialJNI.fact(Native Method)
at factorial.fact(factorial.java:12)
at Main.main(Main.java:10)
Any other possible solution for same?
Thanks in advance
Related
I'm attempting to write a simple program that calls a function written in a pair of Header and CPP files.
I'm doing this on a Raspberry Pi 3 Model B, and the Geany IDE v1.37.1.
Compile Command:
g++ -Wall -c "%f" -c test.cpp
Build Command:
g++ -Wall -o "%e" "%f" -o test test.cpp
main.cpp:
#include "test.h"
int main()
{
test_function();
return 0;
}
test.h:
#ifndef _test_h_
#define _test_h_
#include <iostream>
void test_function();
#endif
test.cpp:
#include "test.h"
void test_function()
{
std::cout << "hello world";
}
The code above compiles & builds fine, however attempting to run it yields the following error:
./main: not found
(program exited with code: 127)
Perhaps I am messing something up with the Compile & Build Commands?
Thank you for reading my post, any guidance is apprecaited!
Notice the compile command:
-o test
This means that the output binary will be test, so you can execute the application in your terminal or shell via ./test.
I have to create a Library for my work that contains Protobuf elements. It should run on every other PC without installing Protobuf. But I can not make the library run without having also the Protobuf files in the same folder (so I compile this files to and the user have to install Protobuf)
So I created for this a test program to demonstrate my problem:
->alias.proto
syntax = "proto3";
package aliasPackage;
message NumberSave{
int32 number = 1;
}
I created the .h and .cc with
protoc -I. --cpp_out=. alias.proto
->Backbone.h
#include "alias.pb.h"
class Backbone{
public:
Backbone();
~Backbone();
void save_Number(int value);
int get_Number();
private:
aliasPackage::NumberSave number;
};
->Backbone.cpp
#include "../header/Backbone.h"
#include <google/protobuf/stubs/common.h>
Backbone::Backbone()
{
number.set_number(0);
}
Backbone::~Backbone()
{
google::protobuf::ShutdownProtobufLibrary();
}
void Backbone::save_Number(int value)
{
number.set_number(value);
}
int Backbone::get_Number()
{
return number.number();
}
->Backbone.h for the lib
class Backbone{
public:
Backbone();
~Backbone();
void save_Number(int value);
int get_Number();
};
Creating the lib out of the files with this command (I thought about a static one, compiling the all independences into the file, but it seems not to work)
g++ -c --std=c++20 -O3 -DBUILD_SHARED_LIBS=ON -I/usr/local/include *.c* -lpthread -L/usr/local/lib -lprotobuf && ar rcs Backbone.sll *.o
->main.cpp
#include "../libs/lib/Backbone.h"
#include <iostream>
int main(){
auto backbone = new Backbone();
backbone->save_Number(5);
std::cout<<backbone->get_Number()<<std::endl;
delete backbone;
return 0;
}
Compile command
g++ -Wall -Wextra -Wpedantic --std=c++20 -O3 main.cpp -L. -l:../libs/lib/Backbone.sll
What am I doing wrong? What's the right command to create a Lib so that users can use it without installing Protobuf?
I have been trying to compile and run the following very simple bit of code
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
I am compiling on Windows 10 with MinGW 6.3.0 (target x86_64-w64-mingw32) and the thread model (obtained using g++ -v) is POSIX.
I am not getting any compilation error with g++ -Wall -g test.cpp -o test.exe, however I am getting a runtime error when trying to run the exe (entry point of _ZNSt6thread15_M_start_threadESt10unique_ptrINS_3_StateESt14default_deleteIS1_EEPFvve cannot be found).
I also tried compiling with the -pthreador -lpthread flags, but I am getting the same runtime error.
Obviously this seems to be related to the use of std::thread, but I didn't get how to fix this. Am I missing a compilation flag or additional library enabling POSIX thread support on Windows?
Edit: I managed to get it working by changing the compilation command to
g++ -Wall -g test.cpp -o test.exe -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,Bdynamic
I tried your code after making the necessary fixes:
#include <thread>
#include <iostream>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
std::cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
and it worked fine.
The build command was the same as yours:
g++ -Wall -g test.cpp -o test.exe
The output was:
012345678910Press any key to continue . . .
But I did use MinGW-w64 GCC 12.1.0 (fro https://winlibs.com/) instead of the very old version 6.3.0.
Maybe the MinGW-w64 (or was it still the old MinGW) library you were using was simply too old...
I want to write a shared library and I want to get a compiler/linker error if I forgot to implement some functions.
Consider the following case:
test.h
class Test {
public:
Test();
};
test.cpp
#include "test.h"
main.cpp
#include "test.h"
int main() {
new Test();
}
If I create a library with this command gcc -c -fpic test.cpp && g++ -shared -o libtest.so -Wl,--no-undefined -Wl,--no-allow-shlib-undefined test.o there is no error message, but the library is broken. Is there a way to force the creation of a not broken library?
Edit: adding additional flag, but doesn't change result
These codes have been modified:
test.h :
class Test {
public:
Test();
};
test.cpp :
#include "test.h"
Test::Test(){} // you must implement the constructor
You must have to implement the constructor, and if not, you get an error "undefined reference to `Test::Test()'".
main.cpp :
#include <iostream>
#include "test.h"
using namespace std;
int main(void)
{
Test* t = new Test(); // you must define a pointer
cout << "test* was created: " << t << endl;
delete t;
t = nullptr;
return 0;
}
Now all the code is OK. Then we create a shared-library with the following command:
g++ -shared -o test.so -fPIC test.cpp
Finally, we compile the main.cpp file at the same time as referring to the test.so shared-library and get the exe output, by the command below:
g++ -g main.cpp test.so -o test.exe
I have the following c++ code for testing:
#include <lua.hpp>
#include <iostream>
static int dummy(lua_State * L)
{
std::cout << "Test";
return 0;
}
int luaopen_testlib(lua_State * L)
{
lua_register(L,"dummy",dummy);
return 0;
}
I compile it with commands and it gives me no errors:
g++ -Wextra -O2 -c -o testlib.o main.cpp
g++ -shared -o testlib.so testlib.o
But when i try to load it in lua i get undefined symbol error as this:
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require"testlib"
error loading module 'testlib' from file './testlib.so':
./testlib.so: undefined symbol: _Z16lua_pushcclosureP9lua_StatePFiS0_Ei
It seems for me that there is something missing in the g++ commands, but i have been searching solution for whole morning and can't get this simple example to compile.
EDIT:
after few recompilations it returned to:
error loading module 'testlib' from file './testlib.so':
./testlib.so: undefined symbol: luaopen_testlib
which was solved by adding :
extern "C"
{
int luaopen_testlib(lua_State *L)
{
lua_register(L,"dummy",dummy);
return 0;
}
}
The Lua binary is compiled as C code, the library tries to use it as C++. That will not work as C++ does name mangling to support overloading. As C does not support overloading it does not need the name mangling and will not understand mangled names.
The solution to this is to tell the C++ compiler that the Lua functions it is going to interact with are straight C and they need no name mangling.
Also the luaopen_testlib function must be extern "C" as it will be called from C code with no mangling.
extern "C" {
#include <lua.h>
}
#include <iostream>
static int dummy(lua_State * L)
{
(void)L;
std::cout << "Test"<<std::endl;
return 0;
}
extern "C"
int luaopen_testlib(lua_State * L)
{
lua_register(L,"dummy",dummy);
return 0;
}
I ran my test with Lua 5.4.2 and used the following commands to build the library:
g++ -Wall -Wextra -O2 -Isrc -c -fPIC -o testlib.o testlib.cpp
g++ -shared -o testlib.so testlib.o
Note the -Isrc is needed to find lua.h in my test setup and -fPIC was required to use cout in the library (but that may depend on the compiler version used).
and the result is:
Lua 5.4.2 Copyright (C) 1994-2020 Lua.org, PUC-Rio
> require 'testlib'
true ./testlib.so
> dummy
function: 0x7ff07d2a0aa0
> dummy()
Test
>
The Lua version used will, in this case, not make any difference.
Try to use Luabind. Here is the Hello World example
#include <iostream>
#include <luabind/luabind.hpp>
void greet()
{
std::cout << "hello world!\n";
}
extern "C" int init(lua_State* L)
{
using namespace luabind;
open(L);
module(L)
[
def("greet", &greet)
];
return 0;
}
This is how i would compile lua, is not exacly gcc but cmake can use gcc.
.
├── CMakeList.txt (A)
├── main.cpp
├── lua_535
│ ├── CMakeLists.txt (B)
│ └── * lua_content *
main.cpp | Just checks if it works
#include <iostream>
#include <string>
#include "lua.hpp"
int main(){
lua_State * lua = luaL_newstate();
std::string str_acction = "a = 5";
int res = luaL_dostring(lua, str_acction.c_str());
std::cout << "DS State > " << res << std::endl;
lua_close(lua);
return 0;
}
CMakeList.txt (A) | Creates the executable and links the library
cmake_minimum_required(VERSION 3.12)
project(lua_test)
add_executable(main main.cpp)
add_subdirectory("lua_535")
target_link_libraries(main PUBLIC lua_lib)
CMakeList.txt (B) | Joins Lua files into a library
Get the latest lua source files
Extract the content into a sub folder
Add this file into the folder
cmake_minimum_required(VERSION 3.12)
project( lua_lib )
set ( LUA_EMBEDDED ON )
set ( LUA_RUNTIME_MAIN "src/luac.c" )
set (LUA_RUNTIME_SOURCES
"src/lapi.c"
"src/lapi.h"
"src/lauxlib.c"
"src/lauxlib.h"
"src/lbaselib.c"
"src/lbitlib.c"
"src/lcode.c"
"src/lcode.h"
"src/lcorolib.c"
"src/lctype.c"
"src/lctype.h"
"src/ldblib.c"
"src/ldebug.c"
"src/ldebug.h"
"src/ldo.c"
"src/ldo.h"
"src/ldump.c"
"src/lfunc.c"
"src/lfunc.h"
"src/lgc.c"
"src/lgc.h"
"src/linit.c"
"src/liolib.c"
"src/llex.c"
"src/llex.h"
"src/llimits.h"
"src/lmathlib.c"
"src/lmem.c"
"src/lmem.h"
"src/loadlib.c"
"src/lobject.c"
"src/lobject.h"
"src/lopcodes.c"
"src/lopcodes.h"
"src/loslib.c"
"src/lparser.c"
"src/lparser.h"
"src/lprefix.h"
"src/lstate.c"
"src/lstate.h"
"src/lstring.c"
"src/lstring.h"
"src/lstrlib.c"
"src/ltable.c"
"src/ltable.h"
"src/ltablib.c"
"src/ltm.c"
"src/ltm.h"
"src/lua.c"
"src/lua.h"
"src/lua.hpp"
"src/luaconf.h"
"src/lualib.h"
"src/lundump.c"
"src/lundump.h"
"src/lutf8lib.c"
"src/lvm.c"
"src/lvm.h"
"src/lzio.c"
"src/lzio.h"
)
add_library( lua_lib "${LUA_RUNTIME_SOURCES}" )
if( NOT LUA_EMBEDDED)
add_library( lua_lib "${LUA_RUNTIME_MAIN}")
endif()
target_include_directories ( lua_lib PUBLIC "${PROJECT_SOURCE_DIR}/src")
If lua is enbedded src/luac.c should be excluded because conteins a int main(){}