Thread: [MSVC] Is it valid to link against an import .lib generated from an executable?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    [MSVC] Is it valid to link against an import .lib generated from an executable?

    Hi,

    I want to share some code which is defined inside a module from which I build an .exe. I want to reuse it in another .Exe

    I know, the correct way is to factor it out into an extra lib and link it to both modules.

    I discovered, that I can export the needed symbols from the .exe through a .def file, just like as it where an DLL and link it to the other exe. It compiles flawlessly. At runtime it crashes with a memory read error once it touches the linked code. So probably it's not valid what I try to do. Maybe someone is able to explain why it crashes?

    Thank you!

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Run time linking needs to use a DLL not a LIB. LIB's need to be fixed up before they are executable.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think you can "load" an exe, can you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Using Loadlibrary()? Hm, I can try it later...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, you either need a .lib file or do a LoadLibrary to load the actual functions into memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Elysia View Post
    I don't think you can "load" an exe, can you?
    You CAN load it using LoadLibrary(), but MS recommends against executing code in the module, this functionality is provided only so you can access resources in the executable.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hold crap, that code is messed up. Do you ever try to view your message before or after posting it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    yeah i deleted it as soon as I saw what it looked like, sorry. Suffice it to say there is a tutorial in the MSVC 6.0 help file on creating a modeless dialog box.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think there are at least three different solutions that are more appealing than "using a function out of another .exe":
    1. Share the source-code instead of the binary - so if "foo.c" contains your code, both project A and project B would contain a copy of "foo.c" - with the right type of source control, it should even be possible to make the source control understand that it's actually exactly the SAME FILE, just copied to two different places.

    2. Create a static library for "foo.c". Link both A and B to the foo.lib file.

    3. Create a proper DLL for foo.c, and link against foo.lib and distribute foo.dll.

    I understand that if A is an already existing application, you may not be able to use option 3, but option 1 and 2 doesn't actually change A in any externally visible way, so it should be fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Ok I see.
    It was just an experimental thing to me. I would had never used this in a productive environment due to the cleaner solutions matsp mentioned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. possible to link and run with .lib file without .dll file?
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 05-25-2006, 09:05 PM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM