94

I am using Microsoft Visual Studio 2011 Professional Beta

I am trying to run the OpenCV C++ files (http://opencv.willowgarage.com/wiki/Welcome) that I have compiled using cMake & the Visual Studio Complier.

However when I go to debug the project I get 600+ errors most of them being:

error LNK2026: module unsafe for SAFESEH image.

Apparently these files are in the opencv_ffmpeg project but I couldn't find them, I have had a look at the safeseh Safe Exception Handlers page on the Microsoft help page but I couldn't find any definitive answers.

I was wondering if anyone else has had this problem and if they managed to fix it.

5
  • 12
    This happens when you link an .obj or .lib that contains code created by an earlier version of the compiler. Which of course would be common if you downloaded a binary for opencv_ffmpeg instead of the source. You can turn the linker option off but then you'll still have a CRT version incompatibility that can byte. Rebuild the library from source. May 15, 2012 at 13:01
  • @HansPassant What to do when there is no source?
    – GregC
    Sep 30, 2012 at 15:57
  • 4
    The obvious thing: ask the owner of the code to provide you with an update. Sep 30, 2012 at 15:58
  • @HansPassant I hope the supplier is still around to make an update. Sounds like a "truck factor of one" kind of problem.
    – GregC
    Sep 30, 2012 at 16:04
  • @HansPassant I have the source code of a project the owner left behind. In my case, the owner disappeared without a trace. No one knows the owner's whereabouts. I'm looking into updating the project to VS2012. Perhaps you might know how to update the codes, right? If not, whom should I consult? Thanks. Oct 2, 2012 at 17:43

5 Answers 5

162

Disabling option "Image has Safe Exception Handlers" in Project properties -> Configuration Properties -> Linker -> Advanced tab helped me.

8
  • 4
    This had no effect for me. Jan 16, 2015 at 17:04
  • 1
    Success, with VS 2013 Express
    – gbarry
    Sep 8, 2017 at 20:12
  • 2
    Success, VS 2015
    – user707779
    May 4, 2018 at 15:16
  • 1
    Success, VS 2012
    – themadmax
    Jul 5, 2018 at 7:27
  • 4
    Success, VS 2017
    – ke4ktz
    May 6, 2019 at 13:44
65

From the comments:

This happens when you link an .obj or .lib that contains code created by an earlier version of the compiler. Which of course would be common if you downloaded a binary for opencv_ffmpeg instead of the source. You can turn the linker option off but then you'll still have a CRT version incompatibility that can byte. Rebuild the library from source. – Hans Passant May 15 at 13:01  
 
Thanks for the help, it worked – Aaron Thompson May 17 at 14:50

1
  • It can also be caused by, as the error states, modules for which the linker can't find safe exception handlers. Assembly language modules in particular - as discussed in other answers. There is some info about this here. Jul 5, 2017 at 22:17
17

If you got this error while building ZLIB in Visual Studio here is the solution. Look for contrib\masmx86\bld_ml32.bat and add /safeseh as a option

Before

ml /coff /Zi /c /Flmatch686.lst match686.asm
ml /coff /Zi /c /Flinffas32.lst inffas32.asm

After

ml /safeseh /coff /Zi /c /Flmatch686.lst match686.asm
ml /safeseh /coff /Zi /c /Flinffas32.lst inffas32.asm
2
  • I had the same problem with zlib, but had to change props on the .asm files in the solution explorer instead. Jul 4, 2018 at 9:42
  • Similarly, using win32\Makefile.msc, added -safeseh to the ASFLAGS.
    – bonger
    Jun 7, 2021 at 10:03
13

Other way is to add some SEH handler (empty for example) to asm files and compile them with /safeseh option, then compile other code normally with /SAFESEH:YES compiler option.

Empty SEH handler:

.safeseh SEH_handler

SEH_handler   proc
;handler
ret

SEH_handler   endp
4
  • 1
    I didn't bother adding a handler to the 3rd party source (zlib), /safeseh seems enough. This really should be upvoted!
    – mlt
    Nov 25, 2015 at 23:28
  • 2
    Anyone having same problems with zlib see this (CTRL + F safeseh) tannerhelland.com/5076/compile-zlib-winapi-wapi-stdcall
    – codekiddy
    Nov 30, 2015 at 22:21
  • I have added "/safeseh" to the command line in the properties for the .asm file and this worked.
    – Mecanik
    Aug 5, 2020 at 9:15
  • Although not a real answer to the question, this answer was helpful for me. It's important to rebuild the obj files after that with the bat file that is in the same directory.
    – flexw
    Dec 23, 2021 at 15:05
0

Your mileage may vary, but none of the above suggestions worked for me (although I did not try rolling my own asm exception handler).

What did work was to select build target Release/x64.

I am running Windows 10 on a 64-bit machine, and using Visual Studio 2015.

The target Release/Win32 works, too. I guess the main thing is to pick "Release".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.