Windows cmake cannot find memset or other standard functions - c

I am compiling a collection of packages using cmake, often for which I write my own CMakeLists.txt. I have to compile them both using Visual Studio 9 2008 and Visual Studio 14 2015
For VS2015 compilation often completes without problems, but with VS2008 I frequently bump into the problem that cmake doesn't link to or include some standard msvc libraries (or msvc runtime libraries to be specific). As a result, it cannot find memset() if it is called by a module. One of these modules is glew, which has the following output:
Scanning dependencies of target glew
[ 8%] Building C object CMakeFiles/glew.dir/C_/Users/Daniel/OneDrive/src/glew-2.0.0/src/glew.c.obj
glew.c
[ 16%] Building RC object CMakeFiles/glew.dir/C_/Users/Daniel/OneDrive/src/glew-2.0.0/build/glew.rc.res
Microsoft (R) Windows (R) Resource Compiler Version 6.1.7600.16385
Copyright (C) Microsoft Corporation. All rights reserved.
[ 25%] Linking C shared library bin\glew32.dll
Creating library lib\glew32.lib and object lib\glew32.exp
glew.c.obj : error LNK2019: unresolved external symbol memset referenced in function glewContextInit
bin\glew32.dll : fatal error LNK1120: 1 unresolved externals
LINK failed. with 1120
NMAKE : fatal error U1077: 'C:\ProgramData\chocolatey\lib\cmake.portable\tools\cmake-3.6.1-win32-x86\bin\cmake.exe' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Users\Daniel\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Users\Daniel\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\nmake.exe"' : return code '0x2'
Stop.
I know I have to include the libcmt.lib or msvcrt.lib (+headers) to the project, but I have no idea how to. I found some leads here and here but none have lead me anywhere so far.
Is there also a way I could do this when calling cmake from the command line (and thus without having to edit or patch the CMakeLists.txt files themselves)?
Thanks for any insights!

Remove the linker -nodefaultlib option; this can be set in the IDE GUI at Linker / Input / Ignore All Default Libraries / No

Related

Windows SDK issue linking 64-bit exe on Windows 7

I have an exe written in C (not C++ or C#) to call a dll. It compiles with Clang and I'm linking it from the Visual Studio command line using link.exe. This build is on Windows 7. My command string is:
link.exe SxSv.obj /nologo /subsystem:console /MACHINE:X64 /LIBPATH:"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64\" /OUT:SxSv.exe kernel32.lib
Link.exe gives me these errors:
SxSv.obj : error LNK2019: unresolved external symbol __imp_LoadLibraryA referenced in function WinMain
SxSv.obj : error LNK2019: unresolved external symbol __imp_GetProcAddress referenced in function WinMain
SxSv.obj : error LNK2019: unresolved external symbol __imp_FreeLibrary referenced in function WinMain
SxSv.obj : error LNK2019: unresolved external symbol __stdio_common_vsprintf referenced in function _vsnprintf_l
LINK : error LNK2001: unresolved external symbol mainCRTStartup
C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\um\x86\uuid.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
SxSv.exe : fatal error LNK1120: 5 unresolved externals
The problem seems to be this line:
/LIBPATH:"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64\"
and I think the problem is the Windows SDK that is installed. This computer has Visual Studio 2017 CE installed.
My questions are:
It looks like there is only one SDK for Windows 7, released in 2010. Where would I expect to see it if it is installed?
Can I use it to link a 64-bit exe?
3, There are a lot of uuid.lib files installed on this Windows 7 box. Which one would I use for Windows 7 for a 64-bit exe?⌈
I am doing this from the Visual Studio Developer command line, not from the IDE.
Thanks for any help.
EDIT: the question suggested as the duplicate of this one is very helpful but it's not the complete solution to this problem as it does not explain what libraries to link to. The comment below by Hans Passant helps best. First I will transfer this project to Visual Studio and go from there to find the right libraries to link this to.

C - Unable to use MSVC linker to link .obj files from the command line

I have been trying to follow the instructions at http://llvm.org/docs/GettingStartedVS.html#an-example-using-the-llvm-tool-chain, but I run into errors at the linking stage. For some reason, I can compile the "hello world" program all the way to an .exe inside the Visual Studio GUI, but when I run the compiler (either clang or cl) from the command line, I get strange errors. I can create .obj objects just fine, but linking produces errors like the following:
link /DEFAULTLIB:libcmt /VERBOSE hello.obj
Microsoft (R) Incremental Linker Version 12.00.31101.0
Copyright (C) Microsoft Corporation. All rights reserved.
Starting pass 1
Searching libraries
Searching C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB\libcmt.lib:
Finished searching libraries
Finished pass 1
Unused libraries:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB\libcmt.lib
hello.obj : error LNK2019: unresolved external symbol printf referenced in function main
LINK : error LNK2001: unresolved external symbol mainCRTStartup
hello.exe : fatal error LNK1120: 2 unresolved externals
For some reason, it is ignoring the specified library and is unable to locate the relevant libraries for linking. How can I get the linker to resolve the symbols properly?

Visual Studio unresolved symbol _InterlockedCompareExchange

I'm porting some C code from GCC into Visual C++ and I'm running into this error when trying to add SQLite3 as a static lib.
I've compiled SQLite as another Visual Studio project, but when I try to add sqlite3.lib in Properties->Liner->"Additional Dependencies" I get the following error:
error LNK2001: unresolved external symbol _InterlockedCompareExchange | File sqlite3.lib(sqlite3.obj)
It seems to be referring to this function, but that's a bit over my head.
Thanks!
SQLite's system call redirection mechanism tries to access InterlockedCompareExchange through a function pointer. This does not work on x64 architectures, where it is a macro.
This is fixed ([1], [2]) in version 3.8.6.

pdcurses linkage using Visual Studio 2010

This is doing my nut in. I'm just trying to include pdcurses (i.e. ncurses for windows) into a test program. Linkage though is failing.
Using Visual Studio 2010.
I'm fully aware of setting up the correct link additional libraries path and to list the libraries themselves. No joy.
I've tried the various pre-builts on the pdcurses website. No such luck.
I of course resorted to building from source. Using nmake from the Visual Studio command prompt as prescribed. Built ok. Nada on link.
What am I missing. It's maybe just too late on Friday...
Output below:
1>------ Build started: Project: test, Configuration: Debug x64 ------
1>Build started 14/12/2012 16:24:32.
1>InitializeBuildStatus:
1> Touching "x64\Debug\test.unsuccessfulbuild".
1>ClCompile:
1> main.c
1>C:\dep\pdcurses\curses.h(160): warning C4005: 'MOUSE_MOVED' : macro redefinition
1> C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\wincon.h(101) : see previous definition of 'MOUSE_MOVED'
1>main.c(73): warning C4133: 'function' : incompatible types - from 'unsigned short [20]' to 'const char *'
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>main.obj : error LNK2019: unresolved external symbol endwin referenced in function main
1>main.obj : error LNK2019: unresolved external symbol wgetch referenced in function main
1>main.obj : error LNK2001: unresolved external symbol __imp_stdscr
1>main.obj : error LNK2019: unresolved external symbol refresh referenced in function main
1>main.obj : error LNK2019: unresolved external symbol printw referenced in function main
1>main.obj : error LNK2019: unresolved external symbol initscr referenced in function main
1>C:\proj\test.exe : fatal error LNK1120: 6 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.47
Ok. I feel rather stupid. Here is the solution for posterity...
The distributed pdcurses binaries are 32bit. I was trying to build for 64bit.
Fundamentally, I forgot the first rule of linkage: don't mix and match architectures.
More specifically, when attempting to then build from source:
1) beware of starting the wrong Visual Studio command prompt. The default one from the Tools menu is the x86 one. If you just try and run cl.exe you'll find this out.
Instead, run the prompt from the start menu: Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio x64 Win64 Command Prompt (2010)
Invoke the build then by cd'ing to pdcurses directory/win32a and typing:
nmake -f vcwin32.mak DLL=pdcurses.dll
Also, 2) before you compile pdcurse change line 111 of the pdcurses Windows makefile (vcwin32.mak):
cvtres /MACHINE:X64 /NOLOGO /OUT:pdcurses.obj pdcurses.res
(NOTE: i.e. X64 architecture!)
Everything is now consistent. The pdcurses library will build. And it will link with my test app which (as the original output shows) I am building on x64.

How to avoid "error LNK2005:" (already defined stdlib functions) when compiling libpng with Microsoft Visual Studio '08?

(Why I am trying to do this: I have had no luck in using the pre-built binaries for libpng on Windows (despite many hours of trial and error), and therefore am now trying to compile it myself. I found this helpful blog post concerning this, complete with a Microsoft Visual Studio 2008 project file, but unfortunately have still not been able to use it.)
It successfully compiles when linking to the zlib binary found here, but when attempting to link it to a test file I receive the following:
C:\Documents and Settings\Administrator\My Documents>cl "C:\Documents and Settin
gs\Administrator\My Documents\test.c" "C:\Documents and Settings\Administrator\M
y Documents\libpng.lib" -I "C:\Documents and Settings\Administrator\My Documents\include"
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test.exe
test.obj
"C:\Documents and Settings\Administrator\My Documents\libpng.lib"
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _abort already defined in LIBCMT.lib(
abort.obj)
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _fread already defined in LIBCMT.lib(
fread.obj)
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _malloc already defined in LIBCMT.lib
(malloc.obj)
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _free already defined in LIBCMT.lib(f
ree.obj)
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: ___iob_func already defined in LIBCMT
.lib(_file.obj)
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; u
se /NODEFAULTLIB:library
test.exe : fatal error LNK1169: one or more multiply defined symbols found
Could anyone care to shed some light as to how to fix these errors?
The problem is that one of the two projects is using Multi-threaded DLL while the other is using Multi-threaded (without DLL in it) in Project Properties -> Configuration Properties -> C/C++ -> Code generation.
Happened to me several times.
Make sure to "synchronize" that setting even for release builds, and for each possible combination (debug, non debug, etc)

Resources