GCC option not found while constructing .dll - java

I'm trying to create a .dll while following this tutorial (http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html) to use JNI in my eclipse project. There's an issue with my makefile however that may be unrelated to all the JNI stuff.
I'm getting "unknown option: --add-stdcall-alias" when I build (make all). I'm using the Mac GCC Compiler. Here's my console log and make file:
EDIT: So i removed the option that is giving me the error and my build worked. However, I feel unsafe just removing a line of code that I am clueless about. Any one want to tell me the implications of removing this code?
console output:
18:05:33 ** Build of configuration Default for project HPA* Testing *
make all
javah -classpath ../bin HPAProgram
gcc -Wl,--add-stdcall-alias -shared -o hpaprogram.dll HPAProgram.o
ld: unknown option: --add-stdcall-alias
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: ** [hpaprogram.dll] Error 1
18:05:34 Build Finished (took 823ms)
makefile:
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : hpaprogram.dll
# $# matches the target, $< matches the first dependancy
hpaprogram.dll : HPAProgram.o
gcc -Wl,--add-stdcall-alias -shared -o $# $<
# $# matches the target, $< matches the first dependancy
HPAProgram.o : HPAProgram.c++ HPAProgram.h
gcc -I /System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/ -c $< -o $#
# $* matches the target filename without the extension
HPAProgram.h : HPAProgram.class
javah -classpath $(CLASS_PATH) $*
clean :
rm HPAProgram.h HPAProgram.o hpaprogram.dll

I practiced by following the same tutorial, it turned out that it's OK to get rid of "-Wl,--add-stdcall-alias".
BTW, in Mac, you have to use ".dylib" instead of ".so", otherwise you will get error saying "java.lang.UnsatisfiedLinkError: no hello in java.library.path".

Related

JNI make file is not working. Multiple definitions

So I am trying to make a jni library but the make file gives a strange error that I don't really understand.
/tmp/ccPWlMuy.o: In function `Java_GameLogic_setMap':
GameLogic.c:(.text+0x0): multiple definition of `Java_GameLogic_setMap'
/tmp/ccajmgva.o:GameLogic.c:(.text+0x0): first defined here
/tmp/ccPWlMuy.o: In function `Java_GameLogic_hello':
GameLogic.c:(.text+0x1c): multiple definition of `Java_GameLogic_hello'
/tmp/ccajmgva.o:GameLogic.c:(.text+0x1c): first defined here
collect2: ld returned 1 exit status
make: *** [GameLogic.o] Error 1
and the make file is:
# Define a variable for classpath
CLASS_PATH = .
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : libGameLogic.so
# $# matches the target, $< matches the first dependancy
libGameLogic.so : GameLogic.o
gcc -W1 -shared -o $# $<
# $# matches the target, $< matches the first dependancy
GameLogic.o : GameLogic.c GameLogic.h
gcc -fPIC -I /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.38.x86_64/include -I /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.38.x86_64/include/linux -shared GameLogic.c -o libGameLogic.so Map.c GameLogic.c
GameLogic.h: GameLogic.class PlayGame.class Bot.class
javah -classpath $(CLASS_PATH) GameLogic
GameLogic.class: GameLogic.java
javac *.java
PlayGame.class: PlayGame.java
javac *.java
Bot.class: Bot.java
javac *.java
clean :
rm GameLogic.o libGameLogic.so
To run the make file I type in make into the terminal and this is the error that it creates. For this I need to have it so that JNI makes a c call which then calls a method in another c file.
So Java <-> JNI <-> GameLogic <-> Map
In that order.
Any advice is much appreciated many thanks.
gcc -fPIC -I /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.38.x86_64/include -I /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.38.x86_64/include/linux -shared -o libGameLogic.so Map.c GameLogic.c
Map.h:7: warning: array ‘name’ assumed to have one element
Map.h:10: warning: array ‘map’ assumed to have one element
gcc -W1 -shared -o libGameLogic.so GameLogic.o
gcc: GameLogic.o: No such file or directory
gcc: no input files
make: *** [libGameLogic.so] Error 1
-bash-4.1$
You are compiling and linking GameLogic.c twice in this line:
# $# matches the target, $< matches the first dependancy
GameLogic.o : GameLogic.c GameLogic.h
gcc -fPIC -I /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.38.x86_64/include
-I /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.38.x86_64/include/linux
-shared GameLogic.c -o libGameLogic.so Map.c GameLogic.c

How to cross compile my C code containing JNI?

Previously I asked a question related to calling java function at
Calling Java functions from C language
Somehow I did with that.
Now I am planning to cross compile that.
I am facing an issue.
Before cross compiling ( means compiling in the same pc for the same pc)
I used the commands
gcc -I /usr/lib/jvm/java-6-openjdk-amd64/include -I /usr/lib/jvm/java-6-openjdk-amd64/include/linux -L /usr/bin/java -L /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server MyC.c -ljvm ;
That created well.
But now I am trying to cross compile the same for my target arm-none-eabi-
My makefile
CROSS_COMPILE =arm-none-eabi-
JAVA_HOME =/usr/lib/jvm/java-7-openjdk-amd64/
test:
javac -d ./ MyJava.java
$(CROSS_COMPILE)gcc -I $(JAVA_HOME)include \
-I $(JAVA_HOME)include/linux \
-L $(JAVA_HOME)jre/lib/amd64/server \
MyC.c -ljvm
This returning error
arm-none-eabi/bin/ld: warning: library search path "/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server" is unsafe for cross-compilation
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so: file not recognized: File format not recognized
collect2: ld returned 1 exit status
make: *** [test] Error 1

Calling Haskell from Java, dynamic linking error Relocation

I'm having troubles with compiling a standalone library for use by Java (with C++ inbetween).
There is a program in Haskell exporting one function that processes some text and returns it.
The program in Haskell needs some external data (binary file). I'm 'compiling it in' with the help of file-embed package.
When I started the compilation with:
$ ghc -fPIC -dynamic -c -O --make MyModule.hs
It throws the error:
MyModule.hs:239:15:
Dynamic linking required, but this is a non-standard build (eg. prof).
You need to build the program twice: once the normal way, and then
in the desired way using -osuf to set the object file suffix.
This is the place where I use file-embed.
So I tried the proposed approach (compiling twice, changing suffixes):
$ ghc -fPIC -c -O --make MyModule.hs
$ ghc -osuf d.o -fPIC -dynamic -c -O --make MyModule.hs
$ javac -cp javacpp.jar MyModule.java
$java -jar javacpp.jar -Dcompiler.path=ghc -Dcompiler.output.prefix="-optc-O3 -Wall MyModule.d.o -dynamic -fPIC -shared -lstdc++ -lHSrts-ghc7.6.3 -o " -Dcompiler.linkpath.prefix2="-optl -Wl,-rpath," MyModule
And now I'm getting an error that I don't understand:
/usr/bin/ld: MyModule.d.o: relocation R_X86_64_PC32 against undefined symbol `{Directory_with_code}zi{Some_module}_{Some_module}_con_info' can not be used when making a shared object; recompile with -fPIC
Can anyone explain it to me and give some tips on how to solve it?
Just a guess, but maybe you need to different version of HSrts-ghc-7.6.3 since this is a "non-standard" build.

JNI.h header cannot be found

I'm trying to create a JNI application between java and c++ and the include #include is telling me that jni.h cannot be found. I've looked at a half dozen threads and web pages and tried fixes that haven't worked. I don't have anything in the path Library/Framework/JavaVM.framework if that helps, and I can't figure out how to get it.
I'm trying to build a JNI project. I know that jni.h is in the path /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/include but for some reason this makefile isn't working.
EDIT: also, I'm using eclipse if that matters.
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : hpaprogram.dll
# $# matches the target, $< matches the first dependancy
hpaprogram.dll : HPAProgram.o
gcc -Wl,--add-stdcall-alias -shared -o $# $<
# $# matches the target, $< matches the first dependancy
HPAProgram.o : HPAProgram.c++ HPAProgram.h
gcc -I"/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/include" -c $< -o $#
# $* matches the target filename without the extension
HPAProgram.h : HPAProgram.class
javah -classpath $(CLASS_PATH) $*
clean :
rm HPAProgram.h HPAProgram.o hpaprogram.dll
I figured it out. My JavaVM.framework was actually in /System/Library/Framework/JavaVM.framework

Solution for error ClassDefNotFound in Java

I have five class files Servant.class, Server.class, Client.class, TransferRequest.class and TransferResponse.class. My Makefile is at the below. I have this error for any of my class file:
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Makefile:11: recipe for target 'TransferRequest.class' failed
How can I cope with this error? I really tried all solutions which was written here such changing path or sth. This makefile is also 5th or 6 th one. The other well known makefiles gave same error too. I am on Windows machine I use cygwin.
Makefile:
JAVAC=javac
sources = $(wildcard *.java)
classes = $(sources:.java=.class)
all: $(classes)
clean :
rm -f *.class
%.class : %.java
$(JAVAC) $<
Add a classpath (with the -cp) option to your javac.
JAVAC=javac -cp "$CLASSPATH"
Or you could change
$(JAVAC) $<
to add the classpath
$(JAVAC) -cp "$CLASSPATH" $<
This is a problem with javac. Either something is missing from your make file or your java installation is broken.
Find out if you can compile a file by hand with javac to narrow it down.

Categories