Avoiding LNK2005 errors in Visual studio - c

I have these files:
main.c
myLib.h
myLib.c
but when I'm trying to compile the whole project this error returned:
1>myLib.obj : error LNK2005: _start_server already defined in main.obj
1>C:\Users\n3tpum63r\documents\visual studio 2010\Projects\NewMultiPlex2\Debug\main.exe : fatal error LNK1169: one or more multiply defined symbols found
I searched it in Google and found this
"LNK2005 Errors When Link C Run-Time Libraries Are Linked Before MFC Libraries"
but couldn't fix it.
How can this error be fixed ?

Get rid of line #1 of myLib.h - you should never normally be #including a .c file.
Conversely in myLib.c you should probably add #include "myLib.h" at the top of the file - it's not strictly necessary at this point in your project but it's a good habit to get into for when you start "real world" programming.
It also looks like you're missing a bunch of system #includes in myLib.c.
The rest looks more or less OK and it should compile and link with the above change(s).

Related

Why do I get a linker error while I try to compile a C file?

I've had this error for weeks I already made a post about it but it wasn't very clear.
So I am calling a function from a a header file myBmpGris.h and the functions are implemented on the file myBmpGris.c . Here is my main file:
#include<stdio.h>
#include<stdlib.h>
#include "myBmpGris.h"
int main(){
char * image_name = "image_carre.bmp";
BmpImg image = readBmpImage(image_name);
return 0;
I compile by using ggc main.c and I get this error message :
Undefined symbols for architecture x86_64:
"_readBmpImage", referenced from:
_main in main-1c453a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I read a lot of posts about the same error message but none of the answers seem to apply to my case. I'm kind of desperate because a lot of my programs give me the same error. What should I do ?
You need to tell the compiler about all the code files which contain any of the needed functions.
So if you have until now compiled like gcc main.c, then the simplest way of also getting the other file compiled is gcc main.c myBmpGris.c.
You might want to read up on the other things you can helpfully tell the compiler (and other parts of the building), i.e. the possible commandline parameters. Or use one of the available free programming environments. (I am not going to name any. Just use your favorite search engine on "C IDE free" or similar. The first few hits discuss several, try a few, then use the one your friends use, or the one you really like much, much better.)
There are two thing.
Compilation you have included. h file. It means comilper will make entry in symbol table for all used function from included library.
Linking here linker try to get address from library to fill in symbol table created in first step. This cannot be performed in your case. So give full path of library.

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.

LNK2001, again

I looked for similar problems, but the only topic might be the one on the use of a library, which I would avoid... here is my issue, I get this error:
1>Signal generator.obj : error LNK2001: unresolved external symbol "double __cdecl findMaxModulus(double *,int)" (?findMaxModulus##YANPANH#Z)
I am using visual studio professional 2008 to develop a c program. I have a main file and another file with all the functions I wrote, myFunctions.h/c. The problem is that these errors do not come out when I include "myFunctions.c", while they come out when I include "myFunctions.h".
i am doing what i remember from university (i am much more into matlab now), which is
/* Home-made includes */
#include "myType.h"
#include "myFunctions.h"
just after the inclusion of the othe headers (stdlib, math, etc...)
any guess? thank you
The error means the linker could not find the function. This most likely means that you aren't compiling your myFunctions.c file at all. Make sure it's added as a "source file" to the project in Visual Studio.
If you want to try on gcc try to compile both .c files same like this below:
gcc -Wall main.c myfunction.c -o final.out
You are missing the other .c file.So in the linking phase linker could not resolve the external symbol (which is your function). Try as I said.
For Visual Studio: You need to check whether all the source files are checked to make a build. there is an option to include number of files from the several source files.

Compilation error in visual C++

I am using Visual studio 2010 for building C project. My project contains a number of header files,source file and parsers. It uses lex and bason files. I am getting a single error during the compilation and íé the following
abc.y:error C2065: 'INPUT' : undeclared identifier
I tried the solutions I am getting like including
#define WIN32_WINNT >= 0x0501
in my main.c file before the inclusion of any of the header files.I am not able to get rid of this error. Could you please let me know what Can be the reasons for this error?
EDIT
The snippet of code that is showing error is:
list_Cons(0, list_List((POINTER)INPUT)
The surprising thing is that If i alter INPUT into INPUT1, I get the same error. It is stoic to change.
Presumably you read this and this.
#define WIN32_WINNT >= 0x0501 wont work. You should try using #define WIN32_WINNT 0x0501 instead.
Also, check that you are actually #including winuser.h
A C++ compiler cannot process a *.y file. For that you need a yacc / bison program, which does not come included with Visual Studio 2010.
For myself I use CMake which can generate MSVC projects along with other build types. You can tell it that a .y needs to be processed outwith the C/C++ files and it will instruct MSVC to invoke whatever external tools are necessary to preprocess the non-C/C++ parts.

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