Thread: Calling Java Subroutine

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Calling Java Subroutine

    I have a C++ program and I would like to use "HTTP GET" in a subroutine. I more or less have this subroutine in Java, but I don't know how to call it from C++. LIBCURL might be good, but I don't know how to use that either. Are there any good example codes on the web where Java is called from a C++ program? I'm on Windows, if that matters.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Use libcurl. It's easy enough to use. Lots of sample code.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    I have the libcurl header files in a folder in my working directory. I also have libcurl.dll in my working directory. I can't seem to compile the following program:

    Code:
    #include <stdio.h>
    #include "curl/curl.h"
     
    int main(void)
    {
    	CURL *curl;
    	CURLcode res;
    	
    	curl = curl_easy_init();
    	if(curl) {
    	curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
    	res = curl_easy_perform(curl);
    	
    	/* always cleanup */
    	curl_easy_cleanup(curl);
    	}
    return 0;
    }
    I type
    Code:
    gcc http_get.c
    and I get all this stuff about undefined references:

    "undefined reference to '_imp__curl_easy_setopt'"
    "undefined reference to '_imp__curl_easy_init'"
    "undefined reference to '_imp__curl_easy_perform'"
    "undefined reference to '_imp__curl_easy_cleanup'"

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    libcurl should come with libcurl.lib. You need to add this to the libraries in the linker options.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    What compiler are you using? I generally use the #pragma directive instead of going into the linker options. So that would require a
    Code:
    #pragma comment( lib, "libcurl.lib" )
    which should set the linker up and it should work.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    I'm using MinGW.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Options menu then.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lets back up just a second and I let me make an alternative suggestion. Is the crux of the problem simply that your HTTP fetching functions are written in Java? If so, why not just port your code over to C(++)? I think it would be an equally simple option. Though, I do not know your level of programming either. So its impossible to say for certain.

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Or if the web stuff you need to do isn't very complicated there are other solutions... This is one (change the pragma to link options) http://cboard.cprogramming.com/showp...57&postcount=9

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    See there ya go. And even if it is terribly complicated I don't think integrating Java code just within this particular context is the most efficient way to go about things.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    The program already exists in Java. If I port it over to C++, I will probably have to use libcurl. Since I am using MinGW, libcurl is not supported with my compiler and it is thus not guaranteed to work. I could use MSVC++, although I would prefer not to. Perhaps I will need to use MSVC++.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I use gcc and libcurl.... So it should be compatible with mingw. If not, don't use a minimal gcc like mingw, use the real deal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  2. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  3. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  4. calling programs from a JAVA application
    By md4u in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2003, 12:40 PM
  5. Calling all Java programmers...
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-14-2002, 05:43 AM