Microsoft Visual C++ – Compiling for 64-bit

Background

As I tried to run our little ODBC application noticed that we have not created the targeted ODBC Data Source.

Error

Error Image

datasourceNameNotFound_01_20200103_0955AM

Error Text


The driver reported the following diagnostics whilst running SQLConnect

State is IM002

native is 0

text is [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Remediation

Created the datasource and as I thought more, opted to consider 64-bit platforms, as well

Target – x64

Objective

Let us compile our application and target 64-bit bitness.

Outline

  1. Compiler Binary
    • Folder :- C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x64\
    • Binary :- cl.exe
  2. Architecture
    • Bitness
      • 32 Bit

 

Compiling

Code


setlocal

set "_arch=amd64"

set "_arch=x86"

REM Set compiler folder

set "_compilerFolder=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x64\"

set "_base=northwind.SQLBindCol_ref"

REM Set target application

set "_outputFile=%_base%_x64.exe"

call _compile.cmd

endlocal

Output

Output – Image

compile.target_x64_withEnvironmentVariable_x32.01.20200103_1107AM

Output – Text

>call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" -arch=x86 -host_arch=x86
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.12
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27031.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

northwind.SQLBindCol_ref.c
Microsoft (R) Incremental Linker Version 14.16.27031.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:northwind.SQLBindCol_ref_x64.exe
/debug
northwind.SQLBindCol_ref.obj
odbc32.lib
user32.lib
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol strcpy referenced in function extractODBCError
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol __acrt_iob_func referenced in function extractODBCError
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol __stdio_common_vfprintf referenced in function _vfprintf_l
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLAllocHandle referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLBindCol referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLCancel referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLDisconnect referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLFetch referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLFreeHandle referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLSetEnvAttr referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLConnectW referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLExecDirectW referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLGetDiagRecW referenced in function extractODBCError
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol SQLSetConnectAttrW referenced in function main
northwind.SQLBindCol_ref.obj : error LNK2001: unresolved external symbol __GSHandlerCheck
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol __security_check_cookie referenced in function extractODBCError
northwind.SQLBindCol_ref.obj : error LNK2019: unresolved external symbol __security_cookie referenced in function extractODBCError
LINK : error LNK2001: unresolved external symbol mainCRTStartup
C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\um\x86\odbc32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\um\x86\user32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
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'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\LIBCMT.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
northwind.SQLBindCol_ref_x64.exe : fatal error LNK1120: 18 unresolved externals<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>

Output – Explanation

  1. LNK1120
    • northwind.SQLBindCol_ref_x64.exe : fatal error LNK1120: 18 unresolved externals
  2.  LNK4272
    • C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\um\x86\odbc32.lib : warning LNK4272: library machine type ‘x86’ conflicts with target machine type ‘x64’
  3. LINK2001
    • LNK2001 – LINK : error LNK2001: unresolved external symbol mainCRTStartup ( C- Compilation – Error )

Remediation

Outline

Mārtiņš Možeiko

Best help came from Mārtiņš Možeiko.

https://handmade.network/forums/t/2611-getting_the_64_bit_compiler_to_work ( Link )

MārtiņšMožeiko_01_20200103_1025AM

The help is invoke vsDevCmd.bat and pass along two parameters

  1. -arch
    • 32 Bit
      • x86
    • 64 Bit
      • amd64
  2. -host_arch
    • 32 Bit
      • x86
    • 64 Bit
      • amd64

Code

_compile.cmd


setlocal

rem https://handmade.network/forums/t/2611-getting_the_64_bit_compiler_to_work

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" -arch=%_arch% -host_arch=%_arch%

set "_compilerBinary=cl.exe"

set "_sourceCodeFolder=."

set "_sourceCodeFile_01=%_base%.c"

set "_libODBC=odbc32.lib"

set "_libUser=user32.lib"

set "_DEBUG=/Z7"

set "_compilerFlag=%_DEBUG%"

"%_compilerFolder%\%_compilerBinary%" %_sourceCodeFolder%\%_sourceCodeFile_01%  ^
%_libODBC% %_libUser% ^
/Fe:%_outputFile% %_compilerFlag%

endlocal 

compile_x32.cmd


setlocal

set "_arch=x86"

REM Set compiler folder

set "_compilerFolder=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x86\"

set "_base=northwind.SQLBindCol_ref"

REM Set target application

set "_outputFile=%_base%_x32.exe"

call _compile.cmd

endlocal

compile_x64.cmd


setlocal

set "_arch=amd64"

REM Set compiler folder

set "_compilerFolder=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x64\"

set "_base=northwind.SQLBindCol_ref"

REM Set target application

set "_outputFile=%_base%_x64.exe"

call _compile.cmd

endlocal

Application Bitness

Check your application bitness

Image

Microsoft

SysInternals \ sigcheck

Let us use SysInternals sigcheck tool to determine the bitness of our application.

Artifact

Sigcheck is available here

Code – Syntax
sigcheck64 [filename]
Code – Sample
sigcheck64 northwind.SQLBindCol_ref_x32.exe
Output – Image

sigcheck_x32_2020_01_04_0755AM

Code – Sample
sigcheck64 northwind.SQLBindCol_ref_x64.exe
Output – Image
sigcheck_x64_2020_01_04_0758AM

 

Source Code Control

GitHub

  1. Repository
    • DanielAdeniji/ODBCCodeSample
      Link
  2. Project
    • ODBCCodeSample/microsoft/visualStudio.Net/c/northwind/SQLBindCol/
      Link
  3. Files
    • compile_x32
      Link
    • compile_x64
      Link
    • compile_x64_withIncorrectMachineType.cmd
      Link

 

References

  1. Mārtiņš Možeiko
  2. René Nyffenegger

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s