Thread: pasting code form a .txt file...

  1. #31
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    well one probeblem solved. Just confuses me tho...hwo the heck did you get a .lib file. wouldn't you want a .dll file? oh well...

  2. #32
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Also looking at the code you posted, you need to make sure BUILDING_DLL is defined.

  3. #33
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    A .lib file makes it easier to use .dll files. With a .lib file, all you have to do is add the header to your program that uses the .dll, and link to the .lib. Then you can call the exported classes/function normally. It makes it easy

    If you dont have a .lib file (just the .dll), then you have to use the functions:
    LoadLibrary()
    and
    GetProcAddress()
    to call the .dll functions.

  4. #34
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    alright ok scratch the other code. I have 3 files now. The dll.cpp, dll.h, and chichi.cpp (my main file).

    Here is the code for all 3...


    Code:
    dll.h
    
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    
    
    void test();
    
    
    #endif /* _DLL_H_ */

    dll.cpp

    Code:
    /* Replace "dll.h" with the name of your header */
    #include "dll.h"
    #include <windows.h>
    #include <iostream.h>
    #include <conio.h>
    
    void test()
    {
        char player[100];
            
        cout<<"Player: ";
        cin>>player;
        cout<<"\n";
    
        
        if(!strcmpi("hello", player))
        {
            cout<<"hi";
            cout<<"\n\n";
        }
        getch();
    }

    chichi.cpp

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include "dll.h"
    
    int main()
    {
        test();
    }

    Ok I know now that the chichi.cpp won't work. But any sugestions on how to make a library using ONLY dev-cpp or just any plain sugestions?

  5. #35
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Hunter2
    Heh, no problem... always glad to help

    Strangely, when I googled for that link just before posting, I couldn't find it... it seemed to have vanished off the face of Google (it was one of the first links in the first search I did originally)! Good thing it was in my browser history though...
    I found it on google:
    http://www.google.com/search?hl=en&i...ionable+DLL%22

    Maybe it was just temporary.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #36
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    wierd

  7. #37
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I've never used Dev C++, so I cant give you the specifics unfortunately. But here is the steps you need to do (you can probably use google to figure out how to do each step).

    First you need to compile dll.cpp and dll.h into a into a dll. There will be a command line option on Dev C++ that you pass to the linker in order to accomplish this. (For instance on MSVC++ it's /dll).

    Next you will use the dll in chichi.cpp. How you do this depends on whether Dev C++ gives you a .dll only, or and .dll and a .lib. If you have the .lib file, then your chichi code is correct as it is. The only additional step you need is to link or your dll.lib file.

    If you only have the dll file, then your chichi.cpp code needs to be change to this:
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    
    int main()
    {
        void(*ptestfn)();
        HMODULE hMod = LoadLibrary("dll.dll");
        if(!hMod)
            return 0;  // failed to load dll
        // we need to get the address of the test() function that is
        // inside our dll
        ptestfn = (void (*)())GetProcAddress(hMod,"test");
        if(ptestfn)
            ptestfn();  // call function
        
    }
    Notice how if we are just using the dll (not the .lib file) we dont need to include dll.h in chichi.cpp

  8. #38
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    alright I'm doing homework right now so I'll check back in a little bit and you use MSCV++ right? Is it free and were can I get it?

  9. #39
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    MSCV++ right? Is it free
    Im afraid not... thats Microsoft teritory right there, and the CHEAPEST I have ever seen it was $75, and that was an old version.

    Newer versions will run you over $100, well, until the Express edition comes out.

  10. #40
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    alright that wrecked my night, jk. I will make due with dev-cpp.

  11. #41
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    oh and your code up there doesn't work.

    Edit: The question I should ask is how do I make a .lib file with dev-cpp?
    Last edited by Rune Hunter; 09-08-2004 at 07:19 PM.

  12. #42
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sang-Drax: Yes. Actually, I later realized that I had the wrong search string.

    bithub: I'd already done that (add to link tab) but was getting unresolved externals still. It wasn't till a few moments ago that I realized the linker will give a different error for missing library files Ok, well, I sort of fixed the problem by making all the functions in the DLL 'extern "C"' (it'll link properly now, apparently the problem WAS in decorated names I guess). But then there's the problem that, seemingly, overloaded functions are not allowed with this method (I suppose that would have something to do with naming conflicts when the names aren't decorated). I thought in MSVC's docs it said that __declspec(dllexport) eliminates the need for extern "c", or at least solves the decorated name problem. Any ideas?

    **EDIT**
    Rune: I believe it's supposed to generate a .lib alongside the .dll file.

    **EDIT2**
    More strangeness. Apparently now it works without the "extern "C"". Perhaps I had the wrong version of the .lib in the folder...
    Last edited by Hunter2; 09-08-2004 at 07:29 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #43
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    extern C shouldnt matter in this case. extern C is used when you are exporting a C++ function that you want to be called from C code. The extern C command tells the compiler to use normal C naming for the function instead of C++ mangling.

    Rune >> Not sure what you mean by "the code doesnt work". You will need to go into more detail than that

  14. #44
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The extern C command tells the compiler to use normal C naming for the function instead of C++ mangling.
    Right, I was reading up on that in the compiler docs... It's also used for easier .DEF file writing, so you don't have to find the decorated names. Although I still have no idea how the .DEF files work
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #45
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    DEF files just specify what functions are exported by the dll. You can effectively do the same thing by putting __declspec( dllexport ) before your function. Most people use __declspec( dllexport ), and dont use def files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM