First, I've been coding in C# for a few years now, and it's been a long time since I've written any C code. I'm using Visual Studio 2013. I am working on a sample program for an API product my company makes. Most of the program is in one source file & 1 header file. The program has to parse XML, which is returned by the API calls. Showing how to parse XML is not the point of the sample, so I decided the program would define a simple function called GetXmlValue in order to hide the XML parsing details. I defined this function in the main .C file like this:
//---------------------
// XML processing function forward declaration
// Note that the implementation of this functions is outside the scope of this program. You must implement it yourself
// using a real XML library.
//---------------------
// This function must return a value indicating the number of characters that were copied into the buffer.
// If no characters were copied, the return value should be zero. It's OK if the function returns a negative value
// to indicate that the XPath could not be found.
extern int GetXmlValue(char *xml, char *xpath, char *buffer, int bufferSize);
Then, in a separate file called ParseFunction.c, I implemented the function. Everything compiles without error, but I'm getting an "unresolved external symbol" link error:
error LNK2019: unresolved external symbol "int __cdecl GetXmlValue(char *,char *,char *,int)" (?GetXmlValue@@YAHPAD00H@Z) referenced in function "int __cdecl parseCommandType(char *)" (?parseCommandType@@YAHPAD@Z) C:...\LPRSDK_Sample\Sample\Sample.obj
As an experiment, I commented out the line in the function where the error was reported, to see if there was something wrong with that particular line, and the error just moved to the next function where the function was called.
If I move the code for the function into the sample program's C file, everything compiles and links. So it's probably something in my link settings, but I'm not familiar enough with the whole VS 2013 C language settings to even know where to look.
I did check the log file and I see the .obj file for both source files is included, so I don't understand why I'm getting the unresolved external symbol error at all. What have I done wrong?
ParseFunction.o(or.obj) gets linked to your main file?C, another one asC++language) so the linker doesn't find a mangled function symbol?GetXmlValue@@YAHPAD00H@Zor the module is not linked properly (so the linker doesn't find the symbol).