VC++ Standard Library Link Error - c++

I'm having trouble with the VC++ compiler. I've installed the windows sdk with compilers, libraries and headers, and tried to compile a test program like so:
#include <cstdio>
int main(int argc, char* argv[]) {
printf("%s", "Hello world!");
return 0;
}
This is the command line script i'm using to compile:
#echo off
set clpath=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64
set clname=cl.exe
set linkpath=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64
set linkname=link.exe
set libpath=/libpath:"C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib" /libpath:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib"
set includepath=/I "C:\Program Files\Microsoft SDKs\Windows\v7.1\Include" /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"
set subsystem=console
set entry=main
set libfiles=
set cfiles=hello.cpp
set objfiles=hello.obj
set path=%clpath%
%clname% %includepath% /c %cfiles%
set path=%linkpath%
%linkname% %libpath% /subsystem:%subsystem% /entry:%entry% %libfiles% %objfiles%
And here's the output:
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
hello.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
hello.obj : error LNK2019: unresolved external symbol printf referenced in function main
hello.exe : fatal error LNK1120: 1 unresolved externals
The program links fine as long as i'm not trying to reference any standard library functions. I've tried messing around with the NODEFAULTLIB and DEFAULTLIB switches as well as specifying the standard libs to link to directly, to no avail.

Your library paths are wrong. This folder:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib
contains the x86 libraries. You're targeting amd64, so you need the amd64 libraries, from:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64
(You'll also need the correct amd64 library path for the Windows SDK; I don't have v7.1 installed, so I don't know exactly what that is.)
Note that you can configure your environment with all of the correct paths and environment variables simply by running:
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64
On a related note, setting /entry:main is wrong: the entry point needs to be the CRT initialization entry point. If you define a main function, then this is mainCRTStartup. The linker will pick this function as the entry point by default, so you do not need to specify an entry point using /entry.

Try adding libcmt.lib to libfiles=
Click here for more info on linking against C-libs on Windows.
BTW. The major advantage of running VC++ is a good IDE. Why do you need to build it from command-line?

Related

Linking boost with PCL using MS Build Tools and command line

I'm using PLC 1.11 installed from the allinone-msvc2019-win64.exe installer. I'm doing a command-line build using MS Build Tools and NMake. The code I'm testing the build with is the example from "Writing Point Cloud data to PCD files" tutorial. I can compile the resulting source to an object file just fine:
cl /c /Zi /EHsc /MTd /Fe.\intermediate\x86\pcd_write.obj /I"\Program Files\PCL 1.11.0\include\pcl-1.11" /I"\Program Files\PCL 1.11.0\3rdParty\Eigen\eigen3" /I"\Program Files\PCL 1.11.0\3rdParty\Boost\include\boost-1_73" .\src\pcd_write.cpp
But when linking, it tries to link to the wrong ABI tag for the boost library:
link /LIBPATH:"\Program Files\PCL 1.11.0\lib" /LIBPATH:"\Program Files\PCL 1.11.0\3rdParty\Boost\lib" /LIBPATH:"\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\lib\x64" /out:.\bin\x86\pcd_write.exe .\intermediate\x86\pcd_write.obj
Microsoft (R) Incremental Linker Version 14.26.28806.0
Copyright (C) Microsoft Corporation. All rights reserved.
LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc142-mt-sgd-x64-1_73.lib'
Note that it should be libbost_filesystem-vc142-mt-gd-x64-1_73.lib. No "s". I'm assuming that I have a link argument wrong (or possibly a compiler argument wrong). But I can't weed through to figure out what. How do I tell the compiler/linker which Boost library to link to?

How to compile 64Bit DLL in Eclipse CDT (using Windows SDK)?

What I want to be done: Create a 64bit DLL on win7 that interacts with the Windows API including the OLE32.dll.
What I have got:
I am currently stuck. I download Visual Studio 2015 Community Edition. I tried to create a DLL there and on creation it generated everything but no dll. The program did not complain, everything was setup as in the tutorial but no dll file.
So I skipped this and downloaded Eclipse CDT. I set up using the Visual C toolchain and after minor issues I had my DLL generated. So I used Java to fire it up but it complained (I use JNA for integration) and it seams that I have a 32bit DLL.
So I searched the web and read and did stuff but I am totally lost. Since the Visual Studio 2010 Express edition came without a 64bit compiler I am not even sure if the Community edition of Visual Studio supports 64bit dlls this time.
I also have the Windows SDK v7.1 installed. I link to a library file ole32.dll. It contains the method CoCreateInstance which I require. I have to add this manually in order for the compiler to pick it up. If I switch to the 64bit dll version the compiler complains about the CoCreateInstance method can not be linked.
So basically I am lost here. A source on the web gave a work around for the 2010 Express being not able to create x64 DLL and it turned out that the hint was getting windows SDK since it it comes along with a compiler.
Also I noticed that CDT uses Visual Studio 10 files so I guess it is using this compiler.
I set to both compiler and linker the /machine:64 option. But the linker still complains that the obj file from the compiler is 32bit.
Any Ideas?
[Update]
The Compiler prints the following line so basically it is not able to compile 64bit version. Any idea how to replace it? As said it uses the Visual Studio 10 folder but I also have a Visual Studio 12 and 14 folder as well.
cl : Command line warning D9002 : ignoring unknown option '/machine:x64'
Well, I don't think '/machine:x64' is a cl option but it's one of linker option.
If you want to make some dll maybe with windows sdk or visual studio environment.
I am not sure that my solution is fit for yours or not, this is what i did for my 64-bit project to compile with eclipse and windows sdk 7.1.
I have two eclipse IDEs which are 32bit and 64bit neon version.
I choose a 64-bit one. You can check one of my answer about installation here. In my opinion, any version of them will be fine. It is your choice.
First step. run 'Windows SDK 7.1 Command Prompt from the start menu.
then, start my eclipse on the command window after changing 64-bit development level with command as follows.
Second Step. Setting up path variables to my project.
I have my own project with a properties window.
The important variable seems to be both LIB and PATH variable in my point.
I crosschecked this two variable, one was from the windows SDK's prompt window and another was current setting variables after installation.
SDK variables here
My final variables are
LIB:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319;
C:\Windows\Microsoft.NET\Framework\v4.0.30319;
C:\Windows\Microsoft.NET\Framework64\v3.5;
C:\Windows\Microsoft.NET\Framework\v3.5;;
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Lib\amd64;
C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\X64
PATH:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319;
C:\Windows\Microsoft.NET\Framework\v4.0.30319;
C:\Windows\Microsoft.NET\Framework64\v3.5;
C:\Windows\Microsoft.NET\Framework\v3.5;;
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE;
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools;;
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\amd64;
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\VCPackages;;
C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\x64;
C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\x64;
C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin;;
C:\Windows\system32;C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
Third, I modified my linker flag as you see the picture below.
Finally, I compiled my project successfully.
Here is my build log.
Info: Internal Builder is used for build
cl /c /EHs /MD /Zi /nologo /Foutil.obj "..\util.c"
rc /Fo proc_view.res "..\proc_view.rc"
Microsoft (R) Windows (R) Resource Compiler Version 6.1.7600.16385
Copyright (C) Microsoft Corporation. All rights reserved.
cl /c /EHs /MD /Zi /nologo /Foprocss.obj "..\procss.c"
link /machine:x64 /debug /nologo /OUT:Systemer.exe FreeList.obj LinkedList.obj proc_main.obj proc_view.res procss.obj util.obj Kernel32.lib Psapi.lib User32.lib
The last one had done with checking if my executable file was 64-bit or 32-bit with a dumpbin command.
That is all.
I think the Eclipse with MinGW or cygwin might be a better option.
I hope this is right for you.

visual studio compiler how to specify the include path to build cpp

I am using a batch file to try to build my cpp program using Visual Studio's cl.exe.
This is what it contains:
"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\cl.exe" /I "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\" "%1" /Fe "%1.exe"
I want to the compiler to include iostream from the include folder and build my .cpp (%1) as %1.exe.
Instead, I get:
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.60610.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
cl : Command line error D8003 : missing source filename
What am I doing wrong?
Win8.1 x64
Answer:
Get rid of the backslash at the end of the include path (...\...\include")
Do not surround %1 with quotes
no space between /Fe and ":
"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\cl.exe" /I "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include" %1 /Fe"%1.exe"
Do not run cl.exe from a standard command prompt. Use the "Developer Command Prompt" installed with VS 2015. This sets several environment variables for you, specific to your installation.
To read more:
https://msdn.microsoft.com/en-us/library/f35ctcxw.aspx

windows sdk 7.1 64-bit build CloseHandle apparently not in kernel32.lib

I have installed SDK 7.1 and am attempting a port of a C/C++ project from MinGW (32-bits) to the VC 2010 that comes with SDK 7.1 at 64 bits. I am compiling in the command line environment set up for the SDK when it was installed.
I got all the files to compile, but linking defeated me, both when I set up a build with static linking of the C/C++ run-time, and when I set up an alternate build with dynamic linking of the C/C++ run-time.
For example Kernel32.lib is a part of my linkage, and it's here, C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\x64\Kernel32.Lib that I confirmed it's being retrieved from, and yet the following linkage error is one of the many reported by both builds.
error LNK2019: unresolved external symbol __imp__CloseHandle#4
How can these linkage problems be resolved? There is no other x64 Kernel32.lib in the SDK or with the VC 2010 that came along with it.
If you are in the correct command line environment, which can be set up by calling %VS100COMNTOOLS%\..\..\VC\vcvarsall.bat x64 from a command prompt if VS 2010 is installed, then the following simple program can be compiled with just cl test.cpp. kernel32.lib is linked in automatically since all Windows applications require it:
#include <windows.h>
int main()
{
HANDLE h;
CloseHandle(h);
}
It sounds like you are using the 32-bit compiler and specifying the 64-bit kernel32.lib explicitly, which I can reproduce. Note the banner "32-bit C/C++ Optimizing Compiler...":
C:\>cl test.cpp -link "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\Kernel32.Lib"
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
c:\test.cpp(6) : warning C4700: uninitialized local variable 'h' used
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test.exe
"c:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\Kernel32.Lib"
test.obj
test.obj : error LNK2019: unresolved external symbol __imp__CloseHandle#4 referenced in function _main

MSVC can't find its libs

For some purpose that is not really important I used MSVC 2012 in console mode (Windows 8x64) and when I tried to compile "Hello, World!" it didn't find its stabdard library and linker couldn't find some of its lib-files. So in the end I managed to configure both compile and link commands to make them work properly:
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe" -c main.cpp /I
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include"
and for link:
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe"
main.obj
/LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib"
/LIBPATH:"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86"
To sum up I want to know, how can they be configured so that cl found its headers and link found those libs. Please consider that I'm going to use them in Qt Creator, where they don't work and I've just found a way of getting them to work but from outside of Creator and I need to do it by Creators' means (nmake or jom options that is).
You need to call
call %ProgramFiles(x86)%\Microsoft Visual Studio 12.0\VC\vcvarsall.bat
in your command line window. This sets up a usable environment.