Enabling/disabling SAFESEH in VC11? - c++

I created a new solution for zlib 1.2.7, set it up as a static library, added the necessary headers and source files from ./zlib/contrib/minizip and got it to compile just fine. I then linked it against libpng (also as a static lib), which also compiles fine. Then, I linked libpng to my graphics engine project (also a static library)... still no errors.
But as soon as I link my main project to my graphics engine, I get several LNK2026 errors regarding crc32.obj, deflate.obj, inflate.obj, d3dxplane.obj, d3dxvec4.obj and other zlib/directx related modules. It's telling me that they are coming from my graphics engine lib, so I've decided to fix this by adding /SAFESEH:NO to my linker flags for the corresponding projects.
NOTE: I should probably state that all projects are set to target x86, NOT x64.
The problem, however, is now I'm getting:
warning LNK4044: unrecognized option '/SAFESEH'; ignored which results in the same LINK2026 errors in my main project.
If I open up a visual studio command prompt and type LINK /? it confirms that /SAFESEH is a valid linker option. I get the same error when I use /SAFESEH (without the ':NO'), and if I omit it entirely then my main project simply refuses build.
What's going on here? How do I get around this?

by adding /SAFESH:NO to my linker flags
Well, you mis-spelled it. Maybe in your question, maybe in the linker settings, the kind of coincidence that tends to explain why something that should work doesn't work.
It is available without having to type it in, do favor doing it that way. Project + Properties, Linker, Advanced, scroll down the right panel, "Image has Safe Exception Handlers" setting. Use the combobox to select No.

A quick google shows this indicates you need to recompile the offending modules with appropriate compiler settings.
Note that this flag is only works for x86 targets. If you're targeting 64-bit, you'll get this error regardless.
Also, from http://msdn.microsoft.com/en-us/library/9a89h429(v=vs.80).aspx, which may be relevant:
The most common reason for the linker not to be able to produce an
image is because one or more of the input files (modules) to the
linker was not compatible with the safe exception handlers feature. A
common reason for a module to not be compatible with safe exception
handlers is because it was created with a compiler from a previous
version of Visual C++.
It would be helpful if you provided version numbers of your compiler, and how your copy of zlib was built (did you build it yourself, and if so, with the same compiler?).

In Visual Studio 2017
Right click on Project -> Properties -> Configuration Properties -> Linker -> All Options ->
Image Has Safe Exception Handlers = No (/SAFESEH:NO)

Related

How to compile c++ without needing a [duplicate]

My current preferred C++ environment is the free and largely excellent Microsoft Visual Studio 2005 Express edition. From time to time I have sent release .exe files to other people with pleasing results. However recently I made the disturbing discovery that the pleasing results were based on more luck that I would like. Attempting to run one of these programs on an old (2001 vintage, not scrupulously updated) XP box gave me nothing but a nasty "System cannot run x.exe" (or similar) message.
Some googling revealed that with this toolset, even specifying static linking results in a simple hello-world.exe actually relying on extra .dll files (msvcm80.dll etc.). An incredibly elaborate version scheming system (manifest files anyone?) then will not let the .exe run without exactly the right .dll versions. I don't want or need this stuff, I just want an old fashioned self contained .exe that does nothing but lowest common denominator Win32 operations and runs on any old win32 OS.
Does anyone know if its possible to do what I want to do with my existing toolset ?
Thank you.
For the C-runtime go to the project settings, choose C/C++ then 'Code Generation'. Change the 'runtime library' setting to 'multithreaded' instead of 'multithreaded dll'.
If you are using any other libraries you may need to tell the linker to ignore the dynamically linked CRT explicitly.
My experience in Visual Studio 2010 is that there are two changes needed so as to not need DLL's. From the project property page (right click on the project name in the Solution Explorer window):
Under Configuration Properties --> General, change the "Use of MFC" field to "Use MFC in a Static Library".
Under Configuration Properties --> C/C++ --> Code Generation, change the "Runtime Library" field to "Multi-Threaded (/MT)"
Not sure why both were needed. I used this to remove a dependency on glut32.dll.
Added later: When making these changes to the configurations, you should make them to "All Configurations" --- you can select this at the top of the Properties window. If you make the change to just the Debug configuration, it won't apply to the Release configuration, and vice-versa.
I've had this same dependency problem and I also know that you can include the VS 8.0 DLLs (release only! not debug!---and your program has to be release, too) in a folder of the appropriate name, in the parent folder with your .exe:
How to: Deploy using XCopy (MSDN)
Also note that things are guaranteed to go awry if you need to have C++ and C code in the same statically linked .exe because you will get linker conflicts that can only be resolved by ignoring the correct libXXX.lib and then linking dynamically (DLLs).
Lastly, with a different toolset (VC++ 6.0) things "just work", since Windows 2000 and above have the correct DLLs installed.
In regards Jared's response, having Windows 2000 or better will not necessarily fix the issue at hand. Rob's response does work, however it is possible that this fix introduces security issues, as Windows updates will not be able to patch applications built as such.
In another post, Nick Guerrera suggests packaging the Visual C++ Runtime Redistributable with your applications, which installs quickly, and is independent of Visual Studio.

C++ boost thread is missing from your computer

I'm having this very strange problem when I try to run my program on code blocks using a g++ compiler. I have installed boost AND used it up until perhaps a week ago, I never had any problems. Today, I returned to a project I was working on and suddenly I get the following error message when I try to run it
The program can't start because libboost_thread-mgw47-mt-1_53.dll is missing from your computer. Try reinstalling the program to fix this problem.
Now I've used boost thread before in this program, so I have no idea why I'm getting this. What's also strange is that boost threads work in other projects I have.
How do I go about trying to solve this? Everything seems to be in place, path variables set, compiler, linker options set, etc... Nothing, that I'm awawre of, has been changed since the last time I ran this program successfully. I did update Visual Studio today, but I don't think that should have any effect. I'm not really sure what other information I can give since I have no idea why this is happening. The .dll file does certainly exist, I've tried moving it into the working directory but still get the same error.
Anyone have any suggestions on how I can go about solving this?
Your compiler in that project is dynamicly linking the boost thread library,
Probably some misconfigured settings in linker options or your code uses dllimport instead of 'normal' functions which are resolved at compile-time (dllimport resolves functions at runtime - it can't find them in the required library so you get an error).
Your other projects work because they staticly link the boost thread library (provide the corret boost preprocessor definisions for static linking/compiling and include the corresponding cpp/lib files for the thread library) so it's included in the program.
So I figured it out. (note again, I'm using code blocks)
I had everything set up in the global compiler and linker settings (which I thought would enough). I had everything set up in the individual projects compiler settings, but the project that was working also had some extra stuff in the linker settings. So I had everything set, EXCEPT the settings in the Project -> Build Options -> Search Directories -> Linker part, with the project name selected, not debug/release. I added in...
$(#boost.lib)
............\boost_1_53_0\stage\lib
and now it's fine.
Sort of surprised that the global compiler settings doesn't cover the individual project settings, but there you go.

Eclipse C/C++ Shows Errors but Compiles?

So I am building some Arduino code in eclipse, as described in Your Second Arduino Project, but every time I use an Arduino library, such as Serial, Eclipse underlines my function names, claiming they cannot be resolved. However, the code actually compiles, so I'm kind of at a loss as to why Eclipse thinks the functions are missing. If anyone has any idea on how to solve this problem it would be appreciated. Thanks beforehand.
EDIT: I should have been more specific, Eclipse underlines the METHODS inside the Arduino libraries. So if I use Serial.println("hello");, it underlines println() and claims it cannot be resolved. Then it compiles just fine and the method works when uploaded to the arduino board.
EDIT2: I found my error, turns out I was trying to use some C++ functions in a C file, and eclipse didn't like it; I renamed to .cpp and all the red disappeared ;) Thanks for your help!
Eclipse may or may not be pulling the paths to index from your build setup, depending on the configuration. Most likely, it is not...it's building correctly because your build setup is just fine, and you can probably build by hand.
The CDT indexer (which is the engine for deciding where all those pretty underlines, as well as code completion, F3 declaration jumping, etc comes from) isn't smart enough in a lot of cases to parse out your Makefiles and know where to look for headers and source. You need to tell Eclipse that information manually.
Go to Project Properties -> C/C++ General -> Paths and Symbols.
The amount of work you need to put into this can vary greatly, depending on your environment. If this external library is the only thing giving you headaches, then you probably just need to add the paths for that library and reindex:
Right-click on the project and select Index -> Rebuild
For starters, what color is the underline? This makes a difference, as yellow means it's a warning, and red means it's an error (critical, will not build in most circumstances).
Second, you need to look at the "Problems" tab to see if there are actual errors. If there is nothing there, then it did indeed compile correctly.
Now, back to the original question. Depending on the type of project you are building, this type of behavior is not that uncommon. Eclipse seems to do a poor job of indexing certain projects. When you run "make all" from the command line (which is effectively what Eclipse does during build) it is likely resolving all of your code and building it just fine.
However, Eclipse uses a different, separate tool for indexing all of your source code and resolving variable/function definitions and declarations. This is literally a case of the left hand not knowing what the right hand is doing.
The solution below worked for me:
Click to your project using right click. Then: Properties -> C/C++ General -> Paths and Symbols -> Symbols -> GNU C++.
Almost for sure there are no symbols at all if you have this problem. Add symbol "__cplusplus" with value "201402L"
After this:
Right click on Project -> Index -> Rebuild
You are done.
I had include folders in
Project Properties -> C/C++ General -> Paths and Symbols -> Includes
When I removed those, the red underlines went away, i.e. the build and the IDE where in sync.
When resolving symbols, CDT indexer seems to consider all header files irrespectively of which ones are actually included in the compilation unit. There is a corresponding bug report filed with Eclipse Bugzilla: https://bugs.eclipse.org/bugs/show_bug.cgi?id=439553
In my case the problem was due to adding "-std=c++17" flag in the language standard field in the project properties under the compile dialect. After that the build was passing with errors, but the program was running fine. So the trick of Index>Rebuild resolved everything.
If you changed something in the configurations, (example, editing in *.cproject file with notepad++) , the below options helps.
Build Configurations --> Clean All and then Index --> Rebuild
I had the same problem.
Index -> Rebuild didn't help.
When I added line #include <avr/iom1280.h> in main.cpp and made Index -> Rebuild underlines dissapeared.
Then I deleted line #include <avr/iom1280.h> and project still without inderlines.
Replace iom1280.h with name of your controller. Look at the "avr\include\avr\" folder for available names
Eclipse does not work as well with C++ as it does with Java, but it should warn you about issues once you press "Rebuild" in the menu bar.
Try that, and see if it resolves your problem.

C++: LINK : debug\XXXXX.exe not found or not built by the last incremental link; performing full link

Using visual studio 2008 SP1,
This line:
LINK : debug\XXXXX.exe not found or not built by the last incremental link; performing full link
appears every single time I compile the project, no matter how small a change I make.
What could be the reasons for that?
Old question, but just in case for someone it is still an issue (and it is..).
Incremental link is incompatible with generating manifest file (Proj opts > Linker > Manifest File > Generate Manifest: Yes). Indeed, generating manifest modifies exe/dll so linker has to do full linkage.
There are some workarounds, for more details:
http://chadaustin.me/2009/05/incremental-linking-and-embedded-manifests/
Temporary (and easiest/fastest) solution is to disable manifest generation during development and enable it again in the release stage. Although this disables XP/Vista-style gui for the app (controls look like in "classic mode").
So it turns out that the problem fixes it self if I add /INCREMENTAL to the linker command line. This in spite the fact that the default behavior according to the docs is to enable incremental linking.
Strange.
Really shooting in the dark but,...
Do you move the XXXXX.exe from where it is built to somewhere else? The whole point of an incremental link is to change an existing exe. If there is none, it will be difficult...
Another possible reason is that the file was changed after the build (probably by another tool)...
All the reasons are listed in the help item for /INCREMENTAL:
Additionally, LINK performs a full
link if any of the following
situations occur:
The incremental status (.ilk) file is
missing. (LINK creates a new .ilk file
in preparation for subsequent
incremental linking.)
There is no write permission for the
.ilk file. (LINK ignores the .ilk file
and links nonincrementally.)
The .exe or .dll output file is
missing.
The timestamp of the .ilk, .exe, or
.dll is changed.
A LINK option is changed. Most LINK
options, when changed between builds,
cause a full link.
An object (.obj) file is added or
omitted.
An object that was compiled with the
/Yu /Z7 option is changed.
Download procmon from Microsoft.
Run it, set up a filter so that you are looking for accesses to the path that contains your .exe name.
Do a link.
See what trouble it's having -- does it find it, does it log an error on opening it. Procmon will log every single file open, read, close, etc. If it gets an error, it will log it.
Also make sure it can find the .ilk file -- I think it needs that as well.
(ALso in the dark) One possible reason is that you use a project-wide header referencing the __DATE__ macro. But in that case, you'd see a full recompile as well (do you?)
In my case, I have got this error yesterday.
VS set code generation > runtime Library to Multi-threaded Debug DLL (/MDd) instead of Multi-threaded Debug (/MTd).
If i recreate new project this bad settings happens again. I manually switch to /Mtd, then no error happens.

How do I make a fully statically linked .exe with Visual Studio Express 2005?

My current preferred C++ environment is the free and largely excellent Microsoft Visual Studio 2005 Express edition. From time to time I have sent release .exe files to other people with pleasing results. However recently I made the disturbing discovery that the pleasing results were based on more luck that I would like. Attempting to run one of these programs on an old (2001 vintage, not scrupulously updated) XP box gave me nothing but a nasty "System cannot run x.exe" (or similar) message.
Some googling revealed that with this toolset, even specifying static linking results in a simple hello-world.exe actually relying on extra .dll files (msvcm80.dll etc.). An incredibly elaborate version scheming system (manifest files anyone?) then will not let the .exe run without exactly the right .dll versions. I don't want or need this stuff, I just want an old fashioned self contained .exe that does nothing but lowest common denominator Win32 operations and runs on any old win32 OS.
Does anyone know if its possible to do what I want to do with my existing toolset ?
Thank you.
For the C-runtime go to the project settings, choose C/C++ then 'Code Generation'. Change the 'runtime library' setting to 'multithreaded' instead of 'multithreaded dll'.
If you are using any other libraries you may need to tell the linker to ignore the dynamically linked CRT explicitly.
My experience in Visual Studio 2010 is that there are two changes needed so as to not need DLL's. From the project property page (right click on the project name in the Solution Explorer window):
Under Configuration Properties --> General, change the "Use of MFC" field to "Use MFC in a Static Library".
Under Configuration Properties --> C/C++ --> Code Generation, change the "Runtime Library" field to "Multi-Threaded (/MT)"
Not sure why both were needed. I used this to remove a dependency on glut32.dll.
Added later: When making these changes to the configurations, you should make them to "All Configurations" --- you can select this at the top of the Properties window. If you make the change to just the Debug configuration, it won't apply to the Release configuration, and vice-versa.
I've had this same dependency problem and I also know that you can include the VS 8.0 DLLs (release only! not debug!---and your program has to be release, too) in a folder of the appropriate name, in the parent folder with your .exe:
How to: Deploy using XCopy (MSDN)
Also note that things are guaranteed to go awry if you need to have C++ and C code in the same statically linked .exe because you will get linker conflicts that can only be resolved by ignoring the correct libXXX.lib and then linking dynamically (DLLs).
Lastly, with a different toolset (VC++ 6.0) things "just work", since Windows 2000 and above have the correct DLLs installed.
In regards Jared's response, having Windows 2000 or better will not necessarily fix the issue at hand. Rob's response does work, however it is possible that this fix introduces security issues, as Windows updates will not be able to patch applications built as such.
In another post, Nick Guerrera suggests packaging the Visual C++ Runtime Redistributable with your applications, which installs quickly, and is independent of Visual Studio.