Microsoft Linker says multiple definition but I need both libraries - linker

Curiously, the last time I worked with this project in 2018, everything was fine. I have no idea what changed. My project is a DLL written in C++. Today when I built it I got messages:
uafxcw.lib(dllmodul.obj) : error LNK2005: _DllMain#12 already defined in LIBCMT.lib(dllmain.obj)
uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2#YAPAXI#Z) already defined in LIBCMT.lib(new.obj)
uafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3#YAXPAX#Z) already defined in LIBCMT.lib(delete.obj)
uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U#YAPAXI#Z) already defined in LIBCMT.lib(new2.obj)
I tried setting /NODEFAULTLIB:LIBCMT.lib and then I got a huge number of unresolved references. So I put LIBCMT.lib back in and used /NODEFAULTLIB:uafxcw.lib, and to the surprise of nobody, got a huge number of unresolved references.None of the conflicts are symbols I've defined myself; they are all library entries. Is there a standard way to resolve these conflicts?Edit 1: Since originally posting, I have also tried adding the linker option /FORCE:MULTIPLE. This seems to work but feels uncomfortable because I don't know if the various definitions lead to the same code and anyway this situation is kind of ugly; I feel that there has to be a better way.

Related

Adding forms to project

I created a project on VC++ 12 and added one form and it worked fine(using the entry point int main(array <string^> ^args) etc... in the frm1.cpp file. The problem arises when I add another form(frm2) to my project(I use the same code in the .cpp as frm1). It gives me
error LNK2005: "int __clrcall sole::main(cli::array^)"
(?main#sole##YMHP$01AP$AAVString#System###Z) already defined in
frm1.obj
AND
error LNK1169: one or more multiply defined symbols found strong text

how to get rid of "LINK : warning LNK4049: locally defined symbol "_xmlFree" imported"?

I build one DLL on windows with microsoft-visual-c 6.0 including the source code from libxml2. Now I have used some xmlFree() calls in my code and I now get the linker warning LNK4049.
I have not the slightest idea, how to get rid of this warning. I googled, but all info I found was above my comprehension (I use normally gcc under solaris). Is there a simple receipt (add/remove compiler-flag or #define/#undef or similar)?
Thanks,
Peter
I was getting this warning when statically linking against libxml2_a.lib with MSVC 11. I haven't tried the DLL version so I'm not sure if it is affected in the same way.
The solution was to define these symbols in your project or makefile to tell the libxml2 header files to assume static linkage and avoid dll-importing and dll-exporting the xmlFree variable at the same time:
LIBXML_STATIC
The same applies to libxslt/libexslt too:
LIBXSLT_STATIC
LIBEXSLT_STATIC
Hope this helps.
I will add bit more context for others who face similar error like following.
We did libxml upgrade from 2.9.4 to 2.9.14 and we observed following errors which are similar to above .
warning LNK4217: locally defined symbol xmlStrcmp imported in function
"void __cdecl ::updateRunElement(struct _xmlNode *,class xml::XmlDoc &,struct _xmlNode *,struct const &)"
(?updateRunElement#YAXPEAU_xmlNode##AEAVXmlDoc#xml#2##2##Z)
LINK : error LNK1218: warning treated as error; no output file generated
warning LNK4217: locally defined symbol xmlFreeDoc imported in function "public: __cdecl xml::XmlDoc::~XmlDoc(void)" (??1XmlDoc#xml##QEAA#XZ)
LINK : error LNK1218: warning treated as error; no output file generated
All the other answers for this error points to set runtime library to /md (Multithreaded dll) or viceversa.
I also tried to suppress these warning by
#pragma warning( disable : 4217 1218) and also by project setting "Disable Specific Warnings" but both did not work as keep giving warnings/errors.
But as #vladimir. (Thanks for the answer) suggested defining LIBXML_STATIC as preprocessor in the project where you are using libxml library either as static or dynamically linked should fix all these warning errors.

Unresolved External Symbol Error When Using cvEigenDecomposition and Others

I am new to learning openCV and was trying to reproduce an online example "face recognition" program here: http://www.cognotics.com/opencv/servo_2007_series/part_5/index.html.
I am however getting an "unresolved external symbol" error for few functions:
cvEigenDecomposite
cvCalcEigenObjects
indNearestNeighbor(float *) // functions defined locally by the user
loadTrainingData(struct CvMat *) // functions defined locally by the user
The "unresolved external symbol" would show up when I mess up the linker. The weird thing, it worked just fine for other examples I have done so far on other websites. My reasoning is that the first two functions cvEigenDecomposite and cvCalcEigenObjects require extra .lib object that I haven't linked to and the other two functions depend on the first two. So my question is whether those functions require extra .lib objects to be linked to?
here is the list of the .lib objects I currently link to:
opencv_core231d.lib
opencv_highgui231d.lib
opencv_imgproc231d.lib
opencv_features2d231d.lib
opencv_calib3d231d.lib
Thanks!
EDIT:
ERRORS:
1>main.obj : error LNK2019: unresolved external symbol _cvEigenDecomposite referenced in function "void __cdecl learn(void)" (?learn##YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _cvCalcEigenObjects referenced in function "void __cdecl doPCA(void)" (?doPCA##YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl findNearestNeighbor(float *)" (?findNearestNeighbor##YAHPAM#Z) referenced in function "void __cdecl recognize(void)" (?recognize##YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl loadTrainingData(struct CvMat * *)" (?loadTrainingData##YAHPAPAUCvMat###Z) referenced in function "void __cdecl recognize(void)" (?recognize##YAXXZ)
1>C:\Users\Lyukshins\Dropbox\Programming\OpenCV2\Face Recognition\Debug\Face Recognition.exe : fatal error LNK1120: 4 unresolved externals
DEPENDENCIES:
C:\opencv\build\include
C:\opencv\build\include\opencv2
C:\opencv\build\include\opencv
INCLUDED DIRECTORIES:
C:\opencv\build\x86\vc10\lib
C:\opencv\build\include
cvEigenDecomposite and cvCalcEigenObjects are located in opencv_legacy231d.lib
The other 2 functions, as you said, are local functions. So they must be declared in the code before the functions that call them.

Prevent Visual Studio from exporting a function in a static library

Odd question: I'm building a static library with Visual Studio and I'm using a source file with a list of utility functions that I also use in the executable project that is importing the static library. As a result I'm getting errors like this:
4>newfuncs.lib(util.obj) : error LNK2005: _shift_left already defined in util.obj
4>newfuncs.lib(util.obj) : error LNK2005: _chop already defined in util.obj
4>newfuncs.lib(util.obj) : error LNK2005: _crc_begin already defined in util.obj
4>newfuncs.lib(util.obj) : error LNK2005: _crc_update already defined in util.obj
4>newfuncs.lib(util.obj) : error LNK2005: _crc_result already defined in util.obj
4>newfuncs.lib(util.obj) : error LNK2005: _strtok_r already defined in util.obj
Anyone know how I could figure out how to get Visual Studio to NOT export the functions in util.obj, since those are natively present in the actual executable project.
The simple answer is to pull the functions in util.obj into their own library and have your library and the executable link it.
If you only need the utility functions in a single source file, you can move them into the .c file and declare them static, then they won't have any linkage outside the file they're defined in.
If you really want to do it by not exporting symbols, mark the function with __declspec(selectany), which will tell VS that multiple definitions of the function are equivalent and it's free to choose whichever it likes (make sure they're actually the same!).
So, figured it out: if you use the pragma statement it will integrate the functions into your static library. In my case I put:
#pragma comment(lib, "libev.lib")
in a header and it imported libev into my library. Now when I actually wrote my executable I only needed to link against my library: there is no additional libev dependency.

Link error LNK2005 when trying to compile several CUDA files together

I have a CUDA program that works fine, but that is currently all written in one file. I'd like to split this big file into several smaller ones, in order to make it easier to maintain and navigate.
The new structure is :
foo.cuh
foo.cu
bar.cuh
bar.cu
main.cu
The .cuh header files contain structs and function prototypes, and the .cu files contain the function definitions (as usual). The main file includes bar.cuh, and bar.cu includes foo.cuh. All the .cu files include cutil_inline.h, in order to be able to use the CUDA functions.
Hence :
// main.cu
#include "bar.cuh"
#include <cutil_inline.h>
int main() [...]
// bar.cu
#include "bar.cuh"
#include "foo.cuh"
#include <cutil_inline.h>
[...]
// foo.cu
#include "foo.cuh"
#include <cutil_inline.h>
[...]
The problem is that when I compile my Visual Studio 2008 project with this new structure, I get tons of link errors :
error LNK2005: "void __cdecl __cutilBankChecker(unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,char *,int,char *,int)" (?__cutilBankChecker##YAXIIIIIIPADH0H#Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cutilCondition(int,char *,int)" (?__cutilCondition##YAXHPADH#Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cutilExit(int,char * *)" (?__cutilExit##YAXHPAPAD#Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "int __cdecl cutGetMaxGflopsDeviceId(void)" (?cutGetMaxGflopsDeviceId##YAHXZ) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cudaSafeCallNoSync(enum cudaError,char const *,int)" (?__cudaSafeCallNoSync##YAXW4cudaError##PBDH#Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cudaSafeCall(enum cudaError,char const *,int)" (?__cudaSafeCall##YAXW4cudaError##PBDH#Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cudaSafeThreadSync(char const *,int)" (?__cudaSafeThreadSync##YAXPBDH#Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cufftSafeCall(enum cufftResult_t,char const *,int)" (?__cufftSafeCall##YAXW4cufftResult_t##PBDH#Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
I understand what they mean (all those symbols already defined are part of cutil_inline.h) but I have to include this header in all files, otherwise it does not compile. What am I doing wrong ?
UPDATE: To clarify the situation :
* with all code in one big file, it compiles, links and runs fine
* with the new structure (several smaller files) and including cutil_inline.h in all .cu files, it compiles correctly but fails during linking
* with the new structure and including cutil_inline.h only in the main file, it fails during compilation, saying that the cutil function are unknown in the files where cutil_inline.h was not included (as expected, but I had to try everything)
- List item
This error also happened in my program. I solved it by adding the keyword inline before __global__ or __device__. Then, the error went away.
Somehow, the functions in cutil_inline.h aren't flagged as "inline" when they are compiled.
If you got this error in a normal non-Cuda C++ project, the answer would simply be that you have function definitions (not just declarations) in the header file and the "inline" keyword in missing.
You might have to generate the corresponding .i files (pre-processor) output to really see what's going on after all macro expansion.
EDIT 1/2/2009
If you can't figure out what's wrong just by reading the .h files, because of some macro expansion obfuscation, here's how you generate the .i file:
In the Visual Studio "Solution
Explorer" window, right-click on the
source file and choose "Properties".
In the properties tree, select
"C/C++", "Preprocessor".
Change the "Generate Preprocessed
File" from "No" to one of the other
options.
Then compile the file. The compiler
will write the preprocessor output
to a file and then stop without
actually compiling. You can see in
the .i file produced what the final
result of all macro expansions is.
You will have to go back and reset
that property back to "No" in order
to get the project to compiler to
work properly again.
Do you need to link with the cutil library (i.e. cutil32D.lib for 32-bit debug etc.)?
For some reason you have multiple definitions. Are you using the NVIDIA Cuda.rules file to enable Visual Studio to compile your .cu files to .obj files? It looks like you have modified the rules to link with cutil, whereas you should use the NVIDIA Cuda.rules to tell VS how to compile .cu to .obj, then modify the standard linker properties to pull in the cutil library.
Consider using 'static' instead of 'inline' to avoid warnings during compilation. That's according to this answer. The causes to this error are discussed here:
However, that's most likely caused by including .cuh files (containing your kernels) into a usual .h file. Either:
make a separate dll filled with your .cuh and .cu files, and link against that;
or rename your .h files to .cuh and .cpp to cu. For this option, make sure to also do this: https://stackoverflow.com/a/20057857/9007125
In the context menu for your .cu file (that you just renamed), select Properties. Then go to General and make sure Item Type is set to CUDA C/C++.
Mind you, the second option will make your project compile a lot slower (compiles 4 times slower)

Resources