Author Topic: Compiling a JNI file in Dev-C++ and Code::Blocks  (Read 21084 times)

Offline kostyabkg

  • Multiple posting newcomer
  • *
  • Posts: 12
Compiling a JNI file in Dev-C++ and Code::Blocks
« on: December 07, 2005, 09:06:55 pm »
Hello!
When I compile a hello.DLL file as it says in JNI tutorial on Java web-site:
http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/index.html, it gives me an unsatisfied linker error. But when I do the same thing in Dev-C++ everything works fine. Also a DLL file produced by Dev-C++ is about 1 KB bigger. There is probably one little check box I need to tick, but I have no idea where this check box is and where to look for it.

Thank you!

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #1 on: December 07, 2005, 09:17:48 pm »
Hello,

Could you post with the link error you get (the C::B build Log)?

Michael

Offline kostyabkg

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #2 on: December 07, 2005, 09:29:23 pm »
I am sorry for being not clear enough. I get this error when I launch the java file. I just followed the tutorial in both cases, but the file compiled by Dev-C++ run correctly while being invoked by my java program, whereas when my java program is trying to invoke the DLL file created by Code::Blocks it gives me this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: displayHelloWorld
        at HelloWorld.displayHelloWorld(Native Method)
        at HelloWorld.main(HelloWorld.java:9)

In this java trail it says about an error, but the error they quote there is a little different from the one I get. It just seems to me that this file somehow doesn't "interact" with my Java program, but what the problem? I don't have a clue... And why Dev-C++ works?

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #3 on: December 07, 2005, 09:36:05 pm »
Ok. Thanks for the explanation.

I have reproduced the same error by following the JNI tutorial. I will try to check what is wrong.

Michael

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #4 on: December 07, 2005, 09:50:11 pm »
That's interesting. If I use the MS Toolkit 2003 compiler all works fine. But with GNU GCC... :?.

Michael

Offline kostyabkg

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #5 on: December 07, 2005, 10:39:48 pm »
int i;
for (i = 0; i < 100; i++)
{
    printf("Thank you!\n");
}

 :D

Yes, it's important to get it working with GNU GCC, because I also want to produce .so files for other systems, than MS Windows. If a Java program loses its' interoperability, then what Java is for???
« Last Edit: December 07, 2005, 10:42:39 pm by kostyabkg »

Offline kostyabkg

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #6 on: December 08, 2005, 10:58:38 am »
I have noticed that "def" files are different if it can help anyhow...

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #7 on: December 08, 2005, 05:56:38 pm »
Hello,

Yesterday night, I have worked to try solve this problem. Interesting challenge :). The problem of the java exception is not IMHO a C::B bug.

Several users have had such issue. Just look at here for descriptions and solutions (which could work or not :?):

http://forum.java.sun.com/thread.jspa?threadID=570194&tstart=29

The problem is that the GNU GCC generates a not correct def file. Therefore, the DLL is not create correctly. As a consequence, Java loads the library, but the method cannot be found (bad signature, I think). This generates the UnsatisfiedLinkError.

Instead MS Toolkit 2003 and (I have read) Borland compiler create a valid DLL (and a valid def/exp file).

In the Java thread mentioned above, a user suggests:

Quote
GOTCHA: I found the option to solve the compiling problem with MinGW gcc. Use this command line:

gcc -shared -o bridgeC.dll bridgeC.o bridgeC.def

Inside the .def file, only describe the right function name:
--- bridgeC.def ----
EXPORT
sayHelloWorld
--- bridgeC.def ----

The compiler will say something like: Warning: linking sayHelloWorld to [email protected] , but the DLL will work.

This solution worked for me too, but only if I call gcc directly by using a command dos window. By using C::B, it did not work. I suppose that I could not configure C::B correctly or I have missed something. I have tried with C::B RC2 and SVN rev1447 with no positive results. I have also used MinGW that come with the release of C::B and another that I have installed with MSYS. Same result.

May be it could be possible to use a makefile, but I did not try it (sorry, but I am not a makefile expert :oops:).

You can try to look at wich compiler/link options use Dev-C++ and import them in C::B. You can also try the same MinGW that Dev-C++ uses.

Best wishes,
Michael

jschwarz0

  • Guest
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #8 on: December 09, 2005, 12:22:37 am »

sorry I don't have this setup to try

from this link: http://forum.java.sun.com/thread.jspa?threadID=470937&messageID=2414695

you should be able to add:

--add-stdcall-alias to your gcc options

or

wrap all jni available calls with

extern C

jim


Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #9 on: December 09, 2005, 10:39:02 am »
you should be able to add:

--add-stdcall-alias to your gcc options

I have already tried this option, but it did not work (at least for me)

or

wrap all jni available calls with

extern C

jim

I will give it a try, thanks.

Michael

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #10 on: December 09, 2005, 10:57:25 am »
wrap all jni available calls with

extern C
Unfortunately, even this does not solve the problem.

Michael

Offline kostyabkg

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #11 on: December 09, 2005, 11:44:49 am »
Yes, the same for me. However it's interesting that in Dev-C++ --add-stdcall-alias is already set in linker options! If I remove it I get the same bloody error with Dev-C++. Maybe it's worth turning to them (Dev-C++ team) and trying to found out, how they managed it to work?

As to me Dev-C++ remains the IDE of my choice. It's friendly for somebody new like me, has a very clean and clear interface. Lots of icons, that help me to remember what i should press to compile the program and what to run it. Also the most of my time I spend with Java, not C, so I need something comfortable to jump to for a few hours and back to Java.

That's something I wanted say about Code::Blocks - I know it's only 1 year old and has an amazing amount of possibilities for a 1 year old IDE, but in my opinion it needs to be a little more friendly to the user. I am sure it won't take much of a programming effort to create BIGGER buttons in a button panel. And also in drop down menus to put the same icon as on the buttons to make memorizing easier (just like they do it MS Office, Dev-C++, ....). I am sure it would attract much more newcomers and spread the popularity of this IDE.

I will be glad to listen to any other opinions.

Thank you very much for the amount of help I received!
I appreciate it, guys.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #12 on: December 09, 2005, 12:15:49 pm »
Yes, the same for me. However it's interesting that in Dev-C++ --add-stdcall-alias is already set in linker options! If I remove it I get the same bloody error with Dev-C++. Maybe it's worth turning to them (Dev-C++ team) and trying to found out, how they managed it to work?

IMHO, the problem is a bit more complex. Even users of Dev-C++ have problems with JNI (see http://forum.java.sun.com/thread.jspa?threadID=570194&tstart=29):

Quote
I am having some problems getting my DLL to work properly. I am following the basic Hello World example and seem to be getting a problem.

I am running on Windows XP with, j2sdk1.4.2_04, and using the gcc that comes with Dev-C++ (Bloodshed). The following is the steps I went through.

...


I think the problem is more from the compiler and/or JNI side, rather than from the IDE.

As to me Dev-C++ remains the IDE of my choice. It's friendly for somebody new like me, has a very clean and clear interface. Lots of icons, that help me to remember what i should press to compile the program and what to run it. Also the most of my time I spend with Java, not C, so I need something comfortable to jump to for a few hours and back to Java.

That's something I wanted say about Code::Blocks - I know it's only 1 year old and has an amazing amount of possibilities for a 1 year old IDE, but in my opinion it needs to be a little more friendly to the user. I am sure it won't take much of a programming effort to create BIGGER buttons in a button panel. And also in drop down menus to put the same icon as on the buttons to make memorizing easier (just like they do it MS Office, Dev-C++, ....). I am sure it would attract much more newcomers and spread the popularity of this IDE.

I do not agree with you, but I respect your choice. I have used Dev-C++ for sometime, but I have had problems e.g., in making it works (it worked in one computer, but not in another without apparent reasons :?). Moreover, it seems that its development is now over, but I may be wrong.

C::B is still young, but it progress fast and new features and/or improvements are added quite each day.

I find C::B user-friendly. Naturally, it needs sometimes to take confidence with the interface, but after 10 minutes I was able to work without many problems.

What is great it is also the possibility for me to compile my code using several compiler (useful for cross-platform developemnt).

Michael

Offline kostyabkg

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #13 on: December 09, 2005, 12:54:10 pm »
Yes, I have noticed as well that somebody was having problems with Dev-C++.

Your opinion is very interesting, I was a bit cuirious why the latest Dev-C++ is of February of this year. It may seem funny or stupid, but the whole reason i started searching for a different IDE is because I found out that Dev-C++ is written in Delphi. There is nothing I have against Delphi, but... CodeBlocks seems to be more professional tool (at least in the future), than Dev-C++. That's why when I came across this error I didn't turn away and get back to Dev-C++, but posted this topic. I have the desire to use CodeBlocks, but I need it to produce correct DLL files or I can't work, so I have to stick with something working, because it's main priority for me.

However it would be grand if somebody could manage to get around this problem without rewriting def file and invoking compiler using the command line.

Is there somebody "Above", who would be able to go into the low-level things and get the solution?

Cheers,
Kostya.

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: Compiling a JNI file in Dev-C++ and Code::Blocks
« Reply #14 on: December 09, 2005, 12:57:10 pm »
I don't remember if there's a way to get Dev-C++ to show the complete command lines it uses when invoking the compiler and linker, but if there is then posting a log of a rebuild might contain enough information to help...