I have a CPP with extern "C" functions. If they are all in a single file, everything works great. I want to split up the functions into different files just for organizational purpose.
So lets say I have these two files:
File_One.cpp
#pragma once
#include "stdafx.h"
#include <windows.h>
#include "Functions.h"
#include "Variables.h"
#include <string>
#include "File_Two.cpp"
extern "C"
{
__declspec(dllexport) void MethodOne()
{
MethodTwo();
}
}
File_Two.cpp
#pragma once
#include "stdafx.h"
#include <windows.h>
#include "Functions.h"
#include "Variables.h"
#include <string>
extern "C"
{
__declspec(dllexport) void MethodTwo()
{
}
}
I have tried rearranging my include headers in different order, and even place no include headers in file_one.cpp other than the include for file_two.cpp but I always get the same errors.
1) error LNK1169: one or more multiply defined symbols found
2) error LNK2005: _MethodTwo already defined in File_One.obj
What exactly am I doing wrong? What should I do to fix it?
Thank you!
You're probably running into issues because you're including the File_two.cpp
file in your File_one.cpp
file. What is happening is that File_two.cpp
and File_one.cpp
are getting compiled and linked. But because File_two.cpp
is included in File_one.cpp
, the linker is seeing two copies of MethodTwo
, and can't decide which to use.
You should move the declarations to a header:
File_one.h:
extern "C"
{
__declspec(dllexport) void MethodOne()
}
And include that instead.
File_two.h:
extern "C"
{
__declspec(dllexport) void MethodTwo();
}
Then define the functions with their body in their respective .cpp
files. No need for extern "C" in the source files.