I have encountered problem with linking static lib - when there no windows.h and calls to WinAPI, it links and works fine, but when they used, I get this error when building:
error: linking with `link.exe` failed: exit code: 1120
note: external-test.lib(library_win32.cpp.obj) : error LNK2019: unresolved external symbol __imp_MessageBoxW referenced in function init_window
In list of all used libs, there no user32.lib. Maybe it is the problem? How can I tell linker to use it
note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX64\\x64\\link.exe" ... "kernel32.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
Here build script:
fn main() {
println!("cargo:rustc-link-search={}/src/external",env::var("CARGO_MANIFEST_DIR").unwrap());
}
Solution was simple - link user32 in build script
Related
I have compiled 32-bit dll library and I have its source and header file. The library is somehow showing print dialog and allows me to print files. I've made 32-bit C# application, which calls printing function in dll and everything is nice.
However, now I need to recompile library to 64-bit (Recompiling my C# project is easy). Basicly I tried two methods.
First one was compiling from Visual's Studio Developer command prompt. This was first time compiling something using command line, so I googled what should I write. I was not attempting to specify 64-bit architecture, because I wanted to be sure that it is working. I used this command to build it.
cl /D_USRDLL /D_WINDLL printing.c User32.Lib WinSpool.Lib Gdi32.Lib ComDlg32.Lib /link /DLL /OUT:printing.dll
It made printing.dll for me, I tried to replace precompiled version I already had and I tried it using my C# app. Everything worked. So I wanted to compile it as 64-bit library. I googled and found out, that I have to use different cl.exe and link.exe located in Program Files\Microsoft Visual Studio 12.0\VC\bin\amd64. So I cd-ed to correct folder and wrote same command. I've got bunch (29) of "unresolved externals", I googled and found out that it is because I am trying to link 32-bit libraries. So I changed all *.Lib-s to C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64*.Lib", retried and still got some (16) unresolved extarnals. Here is the list
/out:printing.exe
/DLL
/OUT:D:\Temp\printing\printing.dll
printing.obj
"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64\User32.Lib"
"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64\WinSpool.Lib"
"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64\Gdi32.Lib"
"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64\ComDlg32.Lib"
Creating library D:\Temp\printing\printing.lib and object D:\Temp\printing\printing.exp
printing.obj : error LNK2019: unresolved external symbol __report_rangecheckfailure referenced in function RSS_PrintFile
printing.obj : error LNK2019: unresolved external symbol strcpy referenced in function RSS_PrintFileBez
printing.obj : error LNK2019: unresolved external symbol strcmp referenced in function RSS_PrintFileBez
printing.obj : error LNK2019: unresolved external symbol strlen referenced in function RSS_PrintFile
printing.obj : error LNK2019: unresolved external symbol __imp_CreateFileA referenced in function RSS_PrintFile
printing.obj : error LNK2019: unresolved external symbol __imp_ReadFile referenced in function RSS_PrintFile
printing.obj : error LNK2019: unresolved external symbol __imp_CloseHandle referenced in function RSS_PrintFile
printing.obj : error LNK2019: unresolved external symbol __imp_GlobalFree referenced in function RSS_PrintFile
printing.obj : error LNK2019: unresolved external symbol __imp_LocalAlloc referenced in function RSS_PrintFileBez
printing.obj : error LNK2019: unresolved external symbol __imp_LocalHandle referenced in function RSS_PrintFileBez
printing.obj : error LNK2019: unresolved external symbol __imp_LocalFree referenced in function RSS_PrintFileBez
printing.obj : error LNK2019: unresolved external symbol atoi referenced in function RSS_PrintFileBezFromTo
printing.obj : error LNK2001: unresolved external symbol __GSHandlerCheck
printing.obj : error LNK2019: unresolved external symbol __security_check_cookie referenced in function RSS_PrintFile
printing.obj : error LNK2019: unresolved external symbol __security_cookie referenced in function RSS_PrintFile
LINK : error LNK2001: unresolved external symbol _DllMainCRTStartup
D:\Temp\printing\printing.dll : fatal error LNK1120: 16 unresolved externals
I managed to get rid of 4 unresolved externals adding /GS- option to compiler, but I do not know how to fix rest and I could not find any help. So I decided to use Microsoft Visual Studio to build my libary.
I've created new C++ project, set it to be a DLL library, added source and header file, compiled (using standart settings, on 32-bit). I've got bunch of warnings like this one
'function' : incompatible types - from 'char *' to 'LPCWSTR'
and simmilars and one error
'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
So I #include _CRT_SECURE_NO_WARNINGS on first line of printing.c file. Hiting "Build" button then resulted in 1 successful and I got my printing.dll. I replaced printing.dll I've got from command line by this one and I tested dll using my C# app. Pressing print button brought "Select Printer and so" window (as before), but hitting print resulted in opening small window with some chinese text. However, I can easily build it as 64-bit.
I would like to ask what am I doing wrong, why I can not compile C library from command prompt and why Microsoft visual studio somehow makes DLL not working.
I tried to debug DLL library compiled using MVS, but I failed. I do not know which solution should I open (C# testing app, or C DLL solution) and what to do next. C# wont show me code from another solution and even if I up debuger in DLL solution to open C# application it produces error
Debugginng information for "app.exe" could not be found or does not match. Skipped loading symbols for NGen binary.
and even if I press "Continue debugging", any breakpoint says, that it wont be hit, because no symbols were loaded.
So Basicly
How do I get rid of "unresolved externals" errors while compiling as 64-bit using command line?
OR
How do I fix Microsoft Visual Studio to built working dll library (does not matter if 32-bit or 64bit)?
Ok. I've managed to fix this issue. I gave up trying to build 64-bit version using command line and focused on fixing issue about chinese chars. I went to Project -> Project Properties -> Configuration Properties -> C/C++ -> Command Line to see what compiler parameters are passed. Then I went back to command line, and added previously found parameters one by one and testing if dll works. When I added /D "UNICODE", the library stopped working.
This being said, adding #undef UNICODE or setting Configuration Properties -> General -> Project Defaults -> Character Set to Not Set fixed my issue and now I a able to compile my printing library for both x86 and x64 architectures.
There are plenty of questions about this link error, but most of it about undefined methods or missing lib files.
I'm trying to compile nginx with my custom module on Windows. I used MSYS with nlink, I set up all environment etc, so nginx without my module compiles and run ok. For some reason I used xxHash algorithm, and I wanted to compile it from source just-in-time, when my module compiles. So I put xxHash folder with its source in subfolder of solution and put #include "xxhash/xxhash.h". It works well when I build standalone test app from VS 2013. However when I do so from nmake, I get linker error
ngx_http_imagick_module.obj : error LNK2019: unresolved external
symbol _XXH32 referenced in function _RunJob ngx_modules.obj : error
LNK2001: unresolved external symbol _ngx_http_imagick_module
objs/nginx.exe : fatal error LNK1120: 2 unresolved externals
My questions:
Why method XXH32 called external at all if it built from source and not statically linked?
Isn't #include directive put physical text from file in place where directive was placed? If so, how it can be that linker searches this symbol somewhere? If not so, shouldn't compiler create something like xxhash.obj? Because I don't see anything similar in compiler log, but I see cl -c -O2 -W4 -nologo -MT -Zi -DFD_SETSIZE=1024 -I "C:/msys/1.0/home/tommi/imagick" -I src/core -I src/event -I src/event/modules -I src/os/win32 -I objs/lib/pcre -I objs/lib/zlib -I objs -I src/http -I src/http/modules -I src/mail -Foobjs/addon/ngx_http_imagick_module/ngx_http_imagick_module.obj ../ngx_http_imagick_module/ngx_http_imagick_module.c?
Should I compile xxHash separately to static lib in order to get this work? It's not a big deal, but I prefer to investigate this issue in order to not face it again later.
Not sure if this relevant, but xxhash has this preprocessor directive in its header:
#if defined (__cplusplus)
extern "C" {
#endif
Can this be reason? If so, how to solve it and why/how VS does this automatically?
I'm trying to compile and run a C code that is using libsndfile library for sound files processing.
I have added the header file using this path:
project's Properties (in C/C++ -> General -> Additional Include Directories)
Also i have linked the library using this path:
Project Properties -> Linker -> Input -> Additional Dependencies.
But while debugging, i get the following errors:
1>sil.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>sil.obj : error LNK2019: unresolved external symbol _sf_write_sync referenced in function _main
1>sil.obj : error LNK2019: unresolved external symbol _sf_write_double referenced in function _main
1>sil.obj : error LNK2019: unresolved external symbol _sf_close referenced in function _main
1>sil.obj : error LNK2019: unresolved external symbol _sf_read_double referenced in function _main
1>sil.obj : error LNK2019: unresolved external symbol _sf_open referenced in function _main
1>c:\users\anam\documents\visual studio 2010\Projects\silence\Debug\silence.exe : fatal error LNK1120: 5 unresolved externals
Can someone please identify the problem here? I'm new at using Visual Studio and I really need help!
I faced the same problem. I solved it as follows.
1)Make sure that you are downloading the 32 bit libsndfile(libsndfile-1.0.25-w32-setup.exe) package because the Visual studio compiler is 32 bit.
2)Install the library by clicking on the setup.exe(libsndfile-1.0.25-w32-setup.exe) file of libsndfile.
3)After creating your project, do the following 2 steps:
i)Go to Debug >> Project Properties >> Configuration Properties >> C/C++ >> General >> Additional Include Directories >> Click on the arrow on the right hand side and then click on edit >> Here, add the path of the include folder in your libsndfile directory.
ii)After step (i) is complete, click on Linker in Configuration Properties >> General >> Additional Library Directories >> Click on the arrow on the right hand side and then click on edit >> Here, add the path of the .lib file. You may have to type in the entire path since the browse option does not show the .lib file. So, in my case I typed C:\Program Files (x86)\Mega-Nerd\libsndfile\lib\libsndfile-1.lib.
Finally, you are done! Your code should compile and work now without throwing any errors.
Missing to specify the folder directory of the library (.lib added to Additional Dependencies)
VC++ Directories view
Click on Library Directories, click the drop-down button on the right and choose and specify the directory it should work.
I had a similar issue and the problem was mixing a x64 lib and a win32 project.
I have written a C Program which calls the function, GetModuleInformation() which is defined in psapi.h
I am using Microsoft Visual Studio C++ command line compiler (cl.exe) for compiling and linking the program.
I have included the psapi.h header file:
#include <psapi.h>
when I try to compile using:
cl program.c
It generates the object file, however fails during the linking stage with the error:
program.obj : error LNK2019: unresolved external symbol _GetModuleInformation#16 ref
erenced in function _main
program.exe : fatal error LNK1120: 1 unresolved externalsprogram.obj : error LNK2019: unresolved external symbol _GetModuleInformation#16 ref
I also place the psapi.lib file in the same folder where the source code file (program.c) is placed, however even then I get the same error message as above.
How do I successfully link it using the command line compiler (cl.exe)?
Method 1
If you want to compile from the command line with cl.exe you can use the /link option to specify linker options :
cl /TC program.c /link psapi.lib
Method 2
The following pragma directive causes the linker to search in your source file for the psapi.lib library while linking .
#pragma comment( lib, "psapi.lib" )
Possible reason for your errors can be, if psapi.lib is missing in a list of additional libraries of linker.
To resolve this, use the following /LIBPATH option :
cl /TC program.c /link Psapi.Lib /LIBPATH:C:\MyLibFolder\
Where C:\MyLibFolder specifies a path to the folder, that contains your psapi.lib .
Also, you can try to set the proper /SUBSYSTEM option .
For a Console application use :
/SUBSYSTEM:CONSOLE
Solution to similar problem here .
Example on using the GetModuleInformation function :
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#pragma comment( lib, "psapi.lib" )
int main(void)
{
MODULEINFO minfo = {0};
GetModuleInformation( GetCurrentProcess(), GetModuleHandle( "psapi.dll" ), &minfo, sizeof(minfo) );
/* printf("%X", minfo.lpBaseOfDll); /* The load address of the module */
return 0;
}
The code has been tested on Windows 7 and XP .
The output from linking session is :
program.c
/out:program.exe
psapi.lib
/LIBPATH:C:\MyLibFolder\
/SUBSYSTEM:CONSOLE
/VERBOSE
program.obj
Starting pass 1
Processed /DEFAULTLIB:uuid.lib
Processed /DEFAULTLIB:LIBCMT
Processed /DEFAULTLIB:OLDNAMES
Searching libraries
Searching C:\MyLibFolder\psapi.lib:
Found _GetModuleInformation#16
Referenced in program.obj
Loaded psapi.lib(PSAPI.DLL)
Found __IMPORT_DESCRIPTOR_PSAPI
Referenced in psapi.lib(PSAPI.DLL)
Loaded psapi.lib(PSAPI.DLL)
Found __NULL_IMPORT_DESCRIPTOR
Referenced in psapi.lib(PSAPI.DLL)
Loaded psapi.lib(PSAPI.DLL)
...
If vsvars32.bat and all appropriate environment variables in your Visual Studio are set correctly the above linker options will produce a valid executable(.exe) file.
I am trying to compile a C program using the Intel icl compiler under MINGW/64. The program uses the following code:
#include <Userenv.h>
HANDLE process;
HANLDE token;
GetUserProfileDirectory(process, TOKEN_QUERY, &ptoken)
I am using the following compile command:
$ icl -g -DMINGW32 -DTESTMAIN user.c -o user -UserEnv.Lib
and I am linking against the UserEnv.Lib from the Microsoft SDK.
Intel(R) C++ Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version Microsoft (R) Incremental Linker Version 9.00.21022.08
-out:user.exe
user.obj
user.obj : error LNK2019: unresolved external symbol __imp_GetUserProfileDirectoryA referenced in function main
Any idea how to solve this problem?
SOLUTION:
The solution is to use
/link /c/Program\ Files/Microsoft\ SDKs/Windows/v6.0A/Lib/x64/UserEnv.Lib /c/Program\ Files/Microsoft\ SDKs/Windows/v6.0A/Lib/x64/A
I copied the file UserEnv.lib from the Microsoft SDK (x64, 6.0) into the current working directory and compiled the program with
$ icl test.c -DMINGW32 ./UserEnv.Lib
LNK2019: unresolved external symbol __imp_GetUserNameA referenced in function main test.obj : error LNK2019: unresolved external symbol __imp_OpenProcessToken referenced in function main test.obj : error LNK2019: unresolved external symbol –
and I am still getting the unresolved symbols.
Olaf
You need to add userenv.lib to your input libraries for the linker to see GetUserProfileDirectory().
EDIT: It's been a while since I've touched an Intel compiler, but IIRC you're supposed to use /link to introduce linker options:
$ icl test.c -DMINGW32 /link ./UserEnv.Lib