I'm trying to link an object file that uses two methods declared in winuser.h and defined in User32.dll: GetMonitorInfo and WindowFromMonitor. The source compiles to an object file just fine, but when I try to link, I get the following error output:
D3dCtx.obj : error LNK2001: unresolved external symbol xGetMonitorInfo
D3dCtx.obj : error LNK2001: unresolved external symbol xMonitorFromWindow
The thing is, I don't call "xGetMonitorInfo" or "xMonitorFromWindow". Running grep on all source files shows that only "GetMonitorInfo" and "WindowFromMonitor" are being called. I'm properly including windows.h, which includes winuser.h. I'm also properly setting my LIBPATH in the linker options, which is confirmed by verbose link output.
The following also appears in my verbose link output:
Found __imp_GetMonitorInfoA
Referenced in nafxcw.lib(afxribboncategory.obj)
Referenced in nafxcw.lib(afxtooltipctrl.obj)
Referenced in nafxcw.lib(afxribbonkeytip.obj)
Referenced in nafxcw.lib(afxfullscreenimpl.obj)
Referenced in nafxcw.lib(afxframeimpl.obj)
Referenced in nafxcw.lib(afxglobalutils.obj)
Referenced in nafxcw.lib(afxdropdowntoolbar.obj)
Referenced in nafxcw.lib(wincore.obj)
Referenced in nafxcw.lib(afxglobals.obj)
Referenced in nafxcw.lib(afxpopupmenu.obj)
Referenced in nafxcw.lib(afxpropertygridtooltipctrl.obj)
Loaded User32.lib(USER32.dll)
Found __imp_MonitorFromWindow
Referenced in nafxcw.lib(wincore.obj)
Loaded User32.lib(USER32.dll)
Furthermore, GetMonitorInfo is defined in winuser.h as:
WINUSERAPI
BOOL
WINAPI
GetMonitorInfoA(
__in HMONITOR hMonitor,
__inout LPMONITORINFO lpmi);
WINUSERAPI
BOOL
WINAPI
GetMonitorInfoW(
__in HMONITOR hMonitor,
__inout LPMONITORINFO lpmi);
#ifdef UNICODE
#define GetMonitorInfo GetMonitorInfoW
#else
#define GetMonitorInfo GetMonitorInfoA
#endif // !UNICODE
When I change all reference to "GetMonitorInfo" to "GetMonitorInfoA", I only get
D3dCtx.obj : error LNK2001: unresolved external symbol xMonitorFromWindow
as my linker error output. Unfortunately, MonitorFromWindow doesn't seem to have multiple versions available.
I should note that I am using the 64bit versions of the libraries, link, and cl.
What's going on here, and how can I successfully link my program?
I don't know whether you were able to find solution to this or not But I had the same problem and the reason this was happening was that I had a file included named multimon.h
Looks like in case of 64 bit compilation, due to Macro definitions, the definitions of these functions are coming from two sources and probably one from multimon.h is overriding and is wrong.
I solved it by just commenting out this include and it has started to link fine.
//#include <multimon.h>
Related
I'm trying to use an import library from a FASM-created DLL to visual studio, no matter what I try I get the unresolved external message.
; proc to export has name exp2
exp2:
....
;
section '.edata' export readable
export 'fasmdll.dll',exp2,'exp2'
The DEF:
LIBRARY FASMDLL
EXPORTS
exp2#4
Then
lib /def:fasmdll.def /out:fasmdll.lib /machine:x86
Then in C++
#pragma comment(lib,"fasmdll.lib")
extern "C" __declspec(dllimport) void __stdcall exp2(DWORD);
The error
dllmain.obj : error LNK2019: unresolved external symbol __imp__exp2#4 referenced in function _exp1#4
If I use __declspec(dllexport) instead I also get
dllmain.obj : error LNK2019: unresolved external symbol _exp2#4
No matter the combination, I can't make it working. Why? In standard Windows libs, the import library wors correctly... I suspect a problem with the DEF file?
I meet an error called "Symbol not found" on matlab. The error message is in below.
Symbol not found: __ZNKSt5ctypeIcE13_M_widen_initEv
Referenced from: blabla/lib/buildW.mexmaci64
Expected in: /usr/lib/libstdc++.6.0.9.dylib
There are quite bunch of questions like me, but I never found the solution of this problem. The former threads mentioned conflicts between the updated clang and the clang used.
Here are exmaples of questions on the similar question.
Need help finding Undefined Symbols
Handling "dyld: lazy symbol binding failed: Symbol not found" error when nm does not find symbol
https://github.com/Homebrew/homebrew-core/issues/4902
I currently installed Xcode 9.2.
And in matlab, I use MacOSX10.13.sdk like below.
>> edit ([matlabroot '/bin/maci64/mexopts/clang++_maci64.xml'])
...
...
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk" />
<cmdReturns name="find $$ -name MacOSX10.13.sdk" />
Also, I found that there are three files in "/usr/lib"
/usr/lib/libstdc++.6.0.9.dylib
/usr/lib/libstdc++.6.dylib
/usr/lib/libstdc++.dylib
Can anyone help me?
The symbol __ZNKSt5ctypeIcE13_M_widen_initEv (demangled std::ctype<char>::_M_widen_init() const) is defined in libstdc++.dylib but if you execute
nm /usr/lib/libstdc++.dylib | fgrep __ZNKSt5ctypeIcE13_M_widen_initEv
you will get
0000000000006a14 t __ZNKSt5ctypeIcE13_M_widen_initEv
as result. The t means that it's defined in the TEXT section but it's a local symbol which cannot referenced from outside the library.
Clang in Xcode uses
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/lib/libstdc++.tbd
for linking instead of /usr/lib/libstdc++.dylib. This is a human readable file which contains only the name of the public symbols in libstdc++.dylib. Since the symbol above is private it is not listed in the .tbd file.
I'm working on developing a Windows CLR form to create GUI interaction for some code I've been handling as a console program.
When I include the header in the console portion of the code, both of my headers play fine together, but when I try to include them in the form, they result in the following:
librarytest.obj: error LNK2005: _SeqWait already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _KillDLL already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _SetSinFreq2 already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _ConnectDirect already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _GetDevice already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _SetSinFreq_Fine2 already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _Connect already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _TacOnTimeForTAction already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _SetSinFreq1 already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _GetLastEAIError already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _SetGain already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _Disconnect already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _ReadFWVer already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _SetSinFreq_Fine1 already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _SetSigSrc already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _ClosePort already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _ShowDebugInfo already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _OpenPort already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _DiscoverDevices already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _TacOnTime already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _PulseOnTime already defined in Gesture_Elicitor.obj
librarytest.obj : error LNK2005: _tactorhandle already defined in Gesture_Elicitor.obj
....
The interesting problem is that one of my headers ("wiimote.h", from the WiiYourself project) works fine if its the only one included. The problem lies with "tactor_cHeader.h", which connects to its .dll . The abbreviated code in question is as follows:
#ifndef TACTOR_H_
#define TACTOR_H_
using namespace std;
#include <windows.h>
...
typedef int (*ConnectDirectPtr)(char*name, int type);
typedef int (*TacOnTimePtr)(int cidx, int board, int tacNum, int durMilli, bool returnifprocessing);
typedef int (*SetFreqPtr)(int cidx, int board, int freq, bool returnifprocessing);
typedef int (*KillDLLptr)();
typedef int (*SeqWaitPtr)(int cidx, int board, int waitTime, bool returnifprocessing);
...
ConnectDirectPtr ConnectDirect;
TacOnTimePtr TacOnTimeForTaction;
SetFreqPtr SetSinFreq1;
SetFreqPtr SetSinFreq2;
KillDLLptr KillDLL;
SeqWaitPtr SeqWait;
...
HINSTANCE tactorhandle = NULL;
inline int InitTactorDLL()
{
tactorhandle = LoadLibrary("Tactor_DLL.dll");
if (tactorhandle == 0)
return -1;
SeqWait = (SeqWaitPtr)GetProcAddress(tactorhandle, "SeqWait");
ConnectDirect = (ConnectDirectPtr)GetProcAddress(tactorhandle, "ConnectDirect");
TacOnTime = (TacOnTimePtr)GetProcAddress(tactorhandle, "TacOnTime");
SetSinFreq1 = (SetFreqPtr)GetProcAddress(tactorhandle, "SetSinFreq1");
SetSinFreq2 = (SetFreqPtr)GetProcAddress(tactorhandle, "SetSinFreq2");
KillDLL = (KillDLLptr)GetProcAddress(tactorhandle, "KillDLL");
}
#endif
So what is it about this header that isn't playing nice with my form?
Sorry for the late reply.
The problem is simple, you have variable definitions inside your header file. Generally, a header file should only contain declarations. Check [SO]: What is the difference between a definition and a declaration? to see the difference between the two.
To fix, you should move these:
ConnectDirectPtr ConnectDirect;
TacOnTimePtr TacOnTimeForTaction;
SetFreqPtr SetSinFreq1;
SetFreqPtr SetSinFreq2;
KillDLLptr KillDLL;
SeqWaitPtr SeqWait;
//...
HINSTANCE tactorhandle = NULL;
into the .c source file that really needs them, or make them extern ([MS.Docs]: extern (C++)).
Background
There are 3 phases when building C (C++) code into Portable Executable code (here I'm referring to .exe and .dll files). For more info, check [MS.Docs]: Peering Inside the PE: A Tour of the Win32 Portable Executable File Format:
1. Preprocess
Done by the preprocessor (cl.exe: [MS.Docs]:
Compiler Options Listed Alphabetically) which is also the compiler (check next phase); this is by default a silent phase (you can see its output by specifying /E, /EP or /P flags)
For every source (.c, .cpp, .cxx, c++, ...) file, it handles all the preprocessing directives ([MS.Docs]: Preprocessor Directives) (e.g.: #define, #if, #include, ...); the result is still a .c (.cpp, ...) file (different than the original, usually, significantly larger) also called compilation unit or translation unit
When an #include directive is encountered, the line that contains the directive (there's only one file included per line of code) is simply replaced by the contents of the (.h or even .c (.cpp, ...)) file included. Note that this is done recursively (if the file included itself contains #include directives they are expanded as well, and so on). The original source file is much smaller than the preprocessed one, which is one of the preprocessor existence reasons
2. Compile
Done by the compiler (check previous phase)
Every translation unit generated at previous phase is converted from C (C++) code (human readable) to machine code (CPU "readable") or COFF format ([MS.Docs]: PE Format). This is the object (.obj) file (its content is gibberish - at least at 1st sight), that can be seen in the VC project's intermediary directory
Note that for each source file included in the project, after this phase there will be a corresponding .obj file
3. Link
Done by the linker (link.exe: [MS.Docs]: Linker Options)
All the object files from the previous phase are merged together (with a bunch of .lib files whose content is similar to .obj file's, but those can only be used indirectly - when building an App) plus some additional operations (e.g. adding the PE sections and headers, relocating some of the code, removing unused code, ...) into the final artifact of the project (the exe or the dll)
Note: this is Win specific, for Nix the phases are (almost) the same, tools differ.
What happens in your code:
The file tactor.h (I'm assuming this is its name, based on the include guard at the beginning) contains a bunch of variable definitions; I'm taking HINSTANCE tactorhandle as an example
Files librarytest.c and Gesture_Elicitor.c (got their names from the linker error) both #include (directly or indirectly) tactor.h
At Phase #1., tactor.h will be expanded in both .c files (independently). So, both translation units will have tactorhandle variable
At Phase #2., the 2 translation units from previous step are compiled and turned into object files, since their code is syntactically correct
At Phase #3., when combining the 2 object files, the linker sees that tactorhandle is present in both of them, and then spits the errors above
Notes:
You can also specify /FORCE:MULTIPLE option to the linker (by adding it to the VC Project Properties -> Linker -> Command Line -> Additional Options), but bear in mind that this might yield unexpected results (it doesn't even work for /clr compiled projects)
Since the project involves .dlls and functions import / export, you could also check [SO]: Linker error when calling a C function from C++ code in different VS2010 project (#CristiFati's answer), for errors that might appear when using this .dll
I'm trying to setup a simple OpenCV project using Microsoft Visual Studio 2008 in a 64-bit Windows 7 system. I don't have admin rights to the system and as such, I can't go for a fresh installation of OpenCV here.
However, I do have the bin, include and lib directories of OpenCV, compiled on a simillar platform.
I have launched a 'Visual C++ -> General -> Empty Project' and created a .cpp file which reads:
#include <opencv/cv.h>
#include <opencv/highgui.h>
void main()
{
cv::Mat frame = cv::imread("D:\\Images\\lena.bmp");
cv::imwrite("D:\\lena_bw.bmp",frame);
}
In the 'Property->Configuration Property->C/C++->General->Additional Include Directories', I have provided the link to the 'include' directory of OpenCV.
In the 'Property->Configuration Property->Linker->Input->Additional Dependencies', I have provided the path to opencv_core230.lib, opencv_highgui230.lib, opencv_imgproc230.lib etc.
The code builds fine, but when I run it, I get:
Unhandled exception at 0x734761e7 in OpenCVTest.exe: 0xC0000005: Access violation reading location 0xcccccccc.
No symbols are loaded for any call stack frame. The source code cannot be displayed.
I'm getting nowehere with my own ideas. I'm kinda new to the Visual Studio platform.
Please help me figure out a way to make this work.
++++++++++++++++++++++++++++++++++++++++++++++++
When running in 'release' mode, I get the following error during compilation:
1>HelloOpenCV.obj : error LNK2001: unresolved external symbol "public: __thiscall cv::_InputArray::_InputArray(class cv::Mat const &)" (??0_InputArray#cv##QAE#ABVMat#1##Z)
1>HelloOpenCV.obj : error LNK2001: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree#cv##YAXPAX#Z)
1>HelloOpenCV.obj : error LNK2001: unresolved external symbol "class cv::Mat __cdecl cv::imread(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?imread#cv##YA?AVMat#1#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z)
1>HelloOpenCV.obj : error LNK2001: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" (?deallocate#Mat#cv##QAEXXZ)
1>HelloOpenCV.obj : error LNK2001: unresolved external symbol "bool __cdecl cv::imwrite(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &,class std::vector<int,class std::allocator<int> > const &)" (?imwrite#cv##YA_NABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##ABV_InputArray#1#ABV?$vector#HV?$allocator#H#std###3##Z)
I am giving you detailed setup instructions that don't need Admin-Rights. Avoid doing things in C drive.
1) Launch VS2010 and select “New project…”.
2) Select “Empty Project”, enter Name, select a location to place project. Press “OK”.
3) Now we have created a project from scratch. It is time to link the libraries and make our project use OpenCV.
Switch to Property manager by clicking on the tab below. If you can’t find the tab, you can access Property manager from menu View.
4) The default properties are for 32-bit systems (note the Win32 suffixes), because you are using 64-bit system, from now on I am going to describe the configuration for 64-bit systems.
If yours is x64 already or you want to work with 32 bit development, leave this step.
From the “Build” menu, select “Configuration Manager”. Under “Active solution platform” Win32 should be selected by default. Click on it and select “New…”.
Select “x64″ as the new platform and select Win32 to copy settings from. Make sure that “Create new project platforms” is checked.
5) INCLUDE
Under “Common Properties” -> “C/C++” -> “General”, edit “Additional Include Directories” and add browse this path to add “..\OpenCV\build\include”. It is important that you select the “include” folder under “build” folder, not any other “include” folder.
6) LINK LIB
Under “Common Properties” -> “Linker” -> “General”, edit “Additional Library Directories” and add “..\OpenCV\build\x64\vc9\lib”. Here “x64″ stands for 64-bit systems, if you are using 32-bit change it to x86. “vc9″ stands for Visual C++ 2008, leave it as it is assuming you are using VS2008, use vc10 for Visual Studio 2010.
7) LIB INPUT
Under “Common Properties” -> “Linker” -> “Input”, edit “Additional Dependencies” and add the following lib files:
[For Debug]
opencv_core242d.lib
opencv_highgui242d.lib
opencv_imgproc242d.lib
[For Release]
opencv_core242.lib
opencv_highgui242.lib
opencv_imgproc242.lib
Here, naming is as: LibName_Version_Debug.lib ie opencv_imgproc242d.lib is opencv_imgproc is library name, 242 is opencv version and d at last indicate debugging library, if you are not using debug mode, don't use library with "d".
I have added very basic libs, you may need to add more according to your code.
8) The steps are the same for “Release” configuration except the linked library filenames. You just need to get rid of the “d” letters before the dot.
Your code should build without any complaints. But while running, you should get an error about a missing dll.
9) DLL
Locate exe/output in your project folder, it's name should be release/debug according to mode of your development and it should have an exe file which is output. Copy the dll files (ends with letter “d” if using debug mode) from “..\OpenCV\build\x64\vc9\bin” (for x64) OR “..\OpenCV\build\x64\vc9\bin” (for 32 bit) and to that folder .
In opencv300 you need opencv_imgcodecs300.lib for imread()/imwrite().
I'm trying to work with the CapSense library for Arduino. However, I can't manage to run the example included with the package, called "CapSenseSketch". When I try just opening the file and running it (I've tried both Arduino 1.0 and 1.0.1), I get a compiler error that says 'CapSense does not name a type", and also
CapSenseSketch.cpp:1:22: error: CapSense.h: No such file or directory
CapSenseSketch:11: error: 'CapSense' does not name a type
CapSenseSketch:12: error: 'CapSense' does not name a type
CapSenseSketch:13: error: 'CapSense' does not name a type
CapSenseSketch.cpp: In function 'void setup()':
CapSenseSketch:17: error: 'cs_4_2' was not declared in this scope
CapSenseSketch.cpp: In function 'void loop()':
CapSenseSketch:24: error: 'cs_4_2' was not declared in this scope
CapSenseSketch:25: error: 'cs_4_6' was not declared in this scope
CapSenseSketch:26: error: 'cs_4_8' was not declared in this scope
There is an include statement at the top of the code,
#include <CapSense.h>
and I thought that would be the problem, so I changed the brackets to quotes and still got an error saying "Error compiling" and:
CapSenseSketch.cpp.o: In function `__static_initialization_and_destruction_0':
CapSenseSketch.cpp:15: undefined reference to `CapSense::CapSense(unsigned char, unsigned char)'
CapSenseSketch.cpp:16: undefined reference to `CapSense::CapSense(unsigned char, unsigned char)'
CapSenseSketch.cpp:17: undefined reference to `CapSense::CapSense(unsigned char, unsigned char)'
CapSenseSketch.cpp.o: In function `loop':
CapSenseSketch.cpp:28: undefined reference to `CapSense::capSense(unsigned char)'
CapSenseSketch.cpp:29: undefined reference to `CapSense::capSense(unsigned char)'
CapSenseSketch.cpp:30: undefined reference to `CapSense::capSense(unsigned char)'
CapSenseSketch.cpp.o: In function `setup':
CapSenseSketch.cpp:21: undefined reference to `CapSense::set_CS_AutocaL_Millis(unsigned long)'
Any ideas? I've worked with CapSense on another computer and changing the brackets to quotes worked fine that time, but it doesn't seem to be doing much now.
Apparently including external files works a bit differently in the arduino ide. It's not enough to simply have an include statement at the top of your code, you must instead manually go to sketch > add file for every external file you want to use. In my case, I only did this for one out of the two files I was trying to use.