locked
linking problems (invalid or corrupt file) RRS feed

  • Question

  • Hello experts!
    I was testing compilation programs with dlls. So I have created simple dll with header:
    int sum(int size)
    {
        return size*2;
    }

    And I have compiled it. I have also created simple console application using this library. In project options I have added headers path and libs path, but I have linking errors:

    Linking... C:\Documents and Settings\Jacek\Moje dokumenty\Visual Studio 2005\Projects\mylib\release\mylib.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x278

    Please help, I don't understand this error and I don't know what to do.
    Greetings
    Thursday, May 25, 2006 2:30 PM

Answers

  • The problem is that you are trying to link the dll directly to your application.  An import lib (mylib.lib) should have been created when you linked the dll.  That's the file you need to link in, not mylib.dll.  That lib will cause your exe to load to your dll at runtime.

    -- Louis Lafreniere

     

     

    Thursday, May 25, 2006 6:10 PM

All replies

  • Are you using Visual C++ 2003 or 2005.

    If 2005, try right clicking the mylib project in the solution explorer and selecting "clean", or (if 2003) try a rebuild of the solution.  If you still have the same problem, post the build output and someone might be able to offer the guidance.

    Thursday, May 25, 2006 2:56 PM
  • I am using VS 2005. After cleaning, problem still remains.
    ------ Build started: Project: middle, Configuration: Release Win32 ------
    Compiling...
    stdafx.cpp
    Compiling...
    test.cpp
    Linking...
    C:\Documents and Settings\Jacek\Moje dokumenty\Visual Studio 2005\Projects\mylib\release\mylib.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x278
    Build log was saved at "file://c:\Documents and Settings\Jacek\Moje dokumenty\Visual Studio 2005\Projects\test\test\Release\BuildLog.htm"
    test - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    • Proposed as answer by Kiet Ly Wednesday, October 10, 2012 4:00 AM
    Thursday, May 25, 2006 3:08 PM
  • I don't see anywhere in your build log that mylib is being built.
    Thursday, May 25, 2006 3:46 PM
  • Mylib is build in other project. Here is log:
    ------ Build started: Project: mylib, Configuration: Release Win32 ------
    Compiling...
    stdafx.cpp
    Compiling...
    mylib.cpp
    Linking...
    Generating code
    Finished generating code
    Creating manifest...
    Build log was saved at "file://c:\Documents and Settings\Jacek\Moje dokumenty\Visual Studio 2005\Projects\mylib\mylib\Release\BuildLog.htm"
    mylib - 0 error(s), 0 warning(s)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    Thursday, May 25, 2006 4:06 PM
  • Your middle project seems to be looking for the mylib dll in a different directory than the one it was built to.  middle is complaining about "C:\Documents and Settings\Jacek\Moje dokumenty\Visual Studio 2005\Projects\mylib\release\mylib.dll" but it appears mylib is being output to "c:\Documents and Settings\Jacek\Moje dokumenty\Visual Studio 2005\Projects\mylib\mylib\Release\".

    You might want remove the reference to mylib.dll and re-add it.

    Thursday, May 25, 2006 4:22 PM
  • After changing that, still the same error.
    Thursday, May 25, 2006 4:26 PM
  • With assemblies that are corrupt or invalid, you should get warnings in the error list when you load your solution/project.  If a referenced DLL is invalid you should also see an exclamation warning sign on the icon for the particular reference in the solution explorer References branch.  Do you get either of those when you load your solution?
    Thursday, May 25, 2006 4:58 PM
  • I am doing that in this way: I ma creating very very simple dll file. Than I create new project and to this project I add header file from this dll. In project options I set additional LIB paths, so linker can find this dll.

    References branch is empty.O_o
    Thursday, May 25, 2006 6:03 PM
  • The problem is that you are trying to link the dll directly to your application.  An import lib (mylib.lib) should have been created when you linked the dll.  That's the file you need to link in, not mylib.dll.  That lib will cause your exe to load to your dll at runtime.

    -- Louis Lafreniere

     

     

    Thursday, May 25, 2006 6:10 PM
  • Unfortunatelly, there is no mylib.lib file in project directory. I am creating dlls in 2 ways.
    1 way: new project-> console application than in project options I set dll.
    2 way: new project-> console application and there I set dll.
    Neither of these methods makes mylib.lib file.
    Thursday, May 25, 2006 6:33 PM
  • If you want to generate a lib file for you mylib project, make sure that "Import Library" is not empty in Project Properties/Linker/Advanced.

    Usually this is set to $(TargetDir)$(TargetName).lib.

    Thursday, May 25, 2006 6:42 PM
  • It is set.
    But command line doesn't contains /IMPLIB

    /OUT:"C:\Documents and Settings\Jacek\Moje dokumenty\Visual Studio 2005\Projects\ezio\Release\ezio.dll" /INCREMENTAL:NO /NOLOGO /DLL /MANIFEST /MANIFESTFILE:"Release\ezio.dll.intermediate.manifest" /DEBUG /PDB:"c:\documents and settings\Jacekszefu\moje dokumenty\visual studio 2005\projects\ezio\release\ezio.pdb" /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /LTCG /MACHINE:X86 /ERRORREPORT:PROMPT kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
    Thursday, May 25, 2006 6:58 PM
  • There seems to be a bug in the property sheet...  Go to the implib setting and touch the implib name (add a space at the end).  When I tried that, the /IMPLIB appeared on the link command line.

    -- Louis Lafreniere

    Thursday, May 25, 2006 8:38 PM
  • .lib file stil doesn't want to appear. But /IMPLIB appeared on the link command line:
    /OUT:"C:\Documents and Settings\Jacek\Moje dokumenty\Visual Studio 2005\Projects\mylibrary\Release\mylibrary.dll" /INCREMENTAL:NO /NOLOGO /DLL /MANIFEST /MANIFESTFILE:"Release\mylibrary.dll.intermediate.manifest" /DEBUG /PDB:"c:\documents and settings\szefu\moje dokumenty\visual studio 2005\projects\mylibrary\release\mylibrary.pdb" /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /LTCG /IMPLIB:"c:\documents and settings\Jacek\moje dokumenty\visual studio 2005\projects\mylibrary\release\mylibrary.lib" /MACHINE:X86 /ERRORREPORT:PROMPT kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
    Friday, May 26, 2006 9:28 AM
  • I have the same problem. Did you have any luck Jacek? So there seem to be 2 release directories, one for the solution and one for the project. The .dll is going into the solution release directory.
    Adding a space to the Linker/Advanced/Import Library in the build config property pages does add the option to what's shown in the Command Line page.
    But the .lib is nowhere to be found.
    Friday, June 2, 2006 9:06 PM
  • Hi,

    I have pretty much the same problem as you, I try to link a dll and it says that it is corrupt. I build it myself and I suspect there's nothing wrong with the dll, the linker just doesn't like it. A workaround that I tried is to link the object files instead of the whole dll and it compiled the program. The problem then is that the DllMain function where I do some initialization is not called. I've still not found any solution to this.

    Angel
    Wednesday, June 7, 2006 7:14 AM
  • Are you sure you are exporting anything from the DLL with __declspec(dllexport)?  If there is nothing exported, the linker will NOT generate a .LIB file.  Unfortunately, the linker will not delete a .LIB file if one exists from a previous build.  That is a recipe for problems and sounds like a bug.

    Wednesday, June 7, 2006 1:30 PM
  • I'm also having the same issue.  My solution has two projects, a dll and a console app.  I'm starting to build a future solution that will have many dll projects, yet I can't get a single one to generate a lib.  Can you eloborate for rusty people like me on how to use the __declspec(dllexport) in order to ensure that
    1) a lib file is generated
    2) dependent projects in the solution link properly to a final build

    My goal is to take dll's and use them in future solutions as portable functionality.

    Thanks

    Saturday, June 24, 2006 2:15 AM
  • Never mind, I found

    http://msdn2.microsoft.com/en-us/library/a90k134d.aspx

    that describes putting __declspec(dllexport) in the header.  Worked like a charm.  I sort of wish this had been pointed out in the documentation, but I could have missed it.

    Thanks for the tip


    Saturday, June 24, 2006 2:23 AM