Visual C++ - one or more multiply defined symbols found - c++

Whenever I start to build my dll I get this error:
fatal error LNK1169: one or more multiply defined symbols found
I think there's nothing wrong with the code because I copied it from a source:
ExoDll1.cpp
#include "stdafx.h"
double BoxArea(double L, double H, double W);
double BoxVolume(double L, double H, double W);
extern "C" __declspec(dllexport)void BoxProperties(double Length, double Height,
double Width, double& Area, double& Volume);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
double BoxArea(double L, double H, double W)
{
return 2 * ((L*H) + (L*W) + (H*W));
}
double BoxVolume(double L, double H, double W)
{
return L * H * W;
}
void BoxProperties(double L, double H, double W, double& A, double& V)
{
A = BoxArea(L, H, W);
V = BoxVolume(L, H, W);
}
I tried to create a new project and delete the old ones but same problem still exists.. What seems to be the problem?

This error message cannot appear with only a single translation unit (such as ExoDll1.cpp). You might be, for example, unknowingly trying to compile multiple versions of this code simultaneously.
Examine your project and get rid of any source code that you do not want to compile.
Make sure that you do not have #include "ExoDll1.cpp" anywhere in your project.

This error comes not from the compiler, but from the linker. This means that the compiler found nothing wrong, in particular, no duplicate definitions of symbols in any one compilation unit. However, the linker, which generates the .dll library, loads several compilation units and found duplicate definitions of symbols across compilation units.
This happens if several compilation units contain the same code with external linkage, i.e. if you #included a source code, or if in a header file (which more than one compilation unit #includes) a function was defined and not declared inline.

Related

A kaldi function only has header file in C++

I tried to find the definition of a function cblas_Xaxpy in Kaldi, so I was directed by GOTO Definition to the final place cblas-wrappers.h, where I found
inline void cblas_Xaxpy(const int N, const float alpha, const float *X,
const int incX, float *Y, const int incY) {
cblas_saxpy(N, alpha, X, incX, Y, incY);
}
Apparently the key is cblas_saxpy, first I tried to direct to the source file of this header file, but I did not find any. So I have tried search the whole directory and the parent directory concerning the project and I could not find any file containing the real definition of cblas_saxpy. But this is the original code and I ran it smoothly.
Then I am confused: if this is the correct version, then there should be some place to define the function cblas_saxpy's implementation, but where is it ?
cblas_saxpy() is defined in the LAPACK library. (As this is a C library, the source code does not need to be present to compile software against the library.) The definition of cblas_saxpy in that library is a wrapper around some extremely old Fortran code.

Unable to use functions with pointer arguments from Visual C++ DLL in VB.NET

I am trying to use a Visual C++ dll in a VB.NET windows application, both created in VS2010. In the Windows project I can successfully add the dll to my references but only functions without pointer arguments are usable in the program and visible in the object browser. I borrowed a simple example I found and changed one of the function arguments to be a pointer.
header file:
// TestDLL.h
using namespace System;
namespace MathFuncs
{
public ref class MyMathFuncs
{
public:
static double Add(double *a, double B);
static double Subtract(double a, double B);
static double Multiply(double a, double B);
static double Divide(double a, double B);
};
}
cpp file
// TestDLL.cpp
// compile with: /clr /LD
#include "TestDLL.h"
namespace MathFuncs
{
double MyMathFuncs::Add(double *a, double B)
{
return *a + b;
}
double MyMathFuncs::Subtract(double a, double B)
{
return a - b;
}
double MyMathFuncs::Multiply(double a, double B)
{
return a * b;
}
double MyMathFuncs::Divide(double a, double B)
{
if (b == 0)
{
throw gcnew DivideByZeroException("b cannot be zero!");
}
return a / b;
}
}
The dll compiles successfully with no warnings. When I reference the dll in a simple windows form I get the error:
"Error 1 'Add' has a return type that is not supported or parameter types that are not supported."
If I remove the pointer the dll works fine. From other forums I thought the calling convention might be a problem and tried using __stdcall but I got another error saying reference classes can't use __stdcall.
I also tried not referencing the dll and instead declaring the dll functions from a module in the windows application. I got an error saying "entry point not found" which I think is because C++ decorates the function name. I tried dumpbin.exe/EXPORTS "dll PATH" but it would not show the decorated function names. I have also tried creating an module definition file and using extern "c" although I most likely didn't use them properly. All solutions I have found to these problems have been for unmanaged C++ but do not work for managed Visual C++.
I would rather be able to reference the dll because it seems simpler. Any insight would be greatly appreciated.
Change your definition to :
double Add(double % a, double b)
{
return a + b;
}
This compiles as ByRef and works:

How to call a function from another header file in C++?

I have the following 3 files (1 *.cpp and 2 *.hpp) :
the main program file:
// test.cpp
#include<iostream>
#include"first_func.hpp"
#include"sec_func.hpp"
int main()
{
double x;
x = 2.3;
std::cout << sec_func(x) << std::endl;
}
-
the first_func.hpp header:
// first_func.hpp
...
double first_func(double x, y, x)
{
return x + y + x;
}
-
the sec_func.hpp header:
// sec_func.hpp
...
double sec_func(double x)
{
double a, b, c;
a = 3.4;
b = 3.3;
c = 2.5;
return first_func(a,b,c) + x;
}
How do I properly call first_func from within the sec_func.hpp file?
For most functions, the implementation should reside in a compilation unit, that is a file that is going to be compiled by itself and compiled once.
Headers are not to be compiled by themselves*, instead they are included by multiple compilation units.
That's why your function definitions should reside in compilation units (like .cpp), not in headers. Headers should contain only the declarations (i.e. without the body), just enough so that other compilation units would know how to call them.
For completeness, the functions that generally need to be defined in headers (as an exception) are:
inline functions
template functions** (classes too)
Footnotes:
* headers can actually be pre-compiled, but that's a solution for speeding up compilation and it doesn't alter their purpose; don't get confused by that.
** you can put template function definitions outside of the headers if you use explicit template instantiation, but that's a rare case; the point is that every compilation unit that wants to instantiate a template (apply arguments to it) needs to have its complete definition, that's why template function definitions go into headers too.
It's a bad practice to place function definition to .hpp files. You should place only function prototypes there. Like this:
first_func.hpp:
double first_func(double x, double y, double x);
first_func.cpp:
double first_func(double x, double y, double x)
{
return x + y + x;
}
The same for second func.
And then, wherever you want to call your first_func, you just include corresponding first_func.hpp in that cpp module, and write the call.
Thus, every your module consists of hpp with all declarations, and cpp with definitions (that is, the bodies). When you need to reference something from this module, you include its hpp and use the name (of constant, variable, function, whatever).
And then you must link everything together:
gcc main.cpp first_func.cpp second_func.cpp -o program
To define a function in a header, you must mark it inline to prevent multiple definitions.
If you want to do this instead of separating the implementation to a separate file, you'll need to provide a prototype before calling the function (either by including the header (prefered) or declaring the function yourself).
// sec_func.hpp
#include "first_func.hpp"
//or
double first_func(double x, y, x); //declaration
double sec_func(double x)
{
double a, b, c;
a = 3.4;
b = 3.3;
c = 2.5;
return first_func(a,b,c) + x;
}

Unresolved External Symbol Occuring with Class Files

First point: I'm not a C++ expert, far from it. I glanced at it briefly almost a year ago and have not touched again until about 2 weeks ago when I decided to teach myself DirectX in C++. I have had my fair share of errors but have (for the most part) been able to solve them myself. I give up on this one though. As far as I can tell the problem resides in how I am using the .h and .cpp files of the mapCube class, so I will post those.
The errors themselves are few but infuriating: I get a LINK:2019 unresolved external symbol error regarding all the functions of mapCube except the constructors, it tells me they are referenced in the main program but claims they are not initialized. The second error I know of has to do with the checkColl function, in that function alone VC++ 2010 decides that x, y, and z are no longer a part of the mapCube class, it is rather perplexing.
The code:
mapCube.h
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
extern const float TILE_WIDTH, TILE_HEIGHT;
extern LPDIRECT3DDEVICE9 d3ddev;
// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")
#ifndef MAPCUBE_H
#define MAPCUBE_H
class mapCube{
struct CUSTOMVERTEX {FLOAT X, Y, Z; D3DVECTOR NORMAL; DWORD COLOR;}; //might be able to put these elsewhere
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE) //they are already in main, but mapCube needs them too
public:
LPD3DXMESH cubeMesh;
float x,y,z;
void setCoord(float, float, float);
D3DXVECTOR3 getCoord(){return D3DXVECTOR3(x,y,z);};
mapCube();
mapCube(float, float, float);
mapCube(float, float, float, D3DXCOLOR);
void draw(D3DXMATRIX);
void setColor(D3DXCOLOR);
int checkColl(D3DXVECTOR3, D3DXVECTOR3);
};
#endif
mapCube.cpp
#include "mapCube.h"
mapCube::mapCube()
{
x=0;
y=0;
z=0;
setColor(D3DCOLOR_XRGB(128,128,128));
}
mapCube::mapCube(float nx, float ny, float nz)
{
x=nx;
y=ny;
z=nz;
setColor(D3DCOLOR_XRGB(128,128,128));
}
mapCube::mapCube(float nx, float ny, float nz, D3DXCOLOR color)
{
x=nx;
y=ny;
z=nz;
setColor(color);
}
void mapCube::setCoord(float nx, float ny, float nz) //this function and the next one are both called
{ //when the cube is created because I'm using
x=nx; //an array of cubes instead of one-by-one
y=ny;
z=nz;
};
void mapCube::setColor(D3DXCOLOR color) //basically just colors each vertex 'color'
{
LPD3DXMESH tmpMesh=NULL;
D3DXCreateBox(d3ddev, TILE_WIDTH, TILE_HEIGHT, TILE_WIDTH, &tmpMesh, NULL);
tmpMesh->CloneMeshFVF( 0, CUSTOMFVF, d3ddev, &cubeMesh );
LPDIRECT3DVERTEXBUFFER9 tmpVertBuf=NULL;
if( SUCCEEDED(cubeMesh->GetVertexBuffer(&tmpVertBuf)))
{
int nNumVerts = cubeMesh->GetNumVertices();
CUSTOMVERTEX *pVertices = NULL;
tmpVertBuf->Lock( 0, 0, (void**)&pVertices, 0 );
{
int i=0;
while(i<nNumVerts)
{
pVertices[i].COLOR=color;
i++;
}
}
tmpVertBuf->Unlock();
tmpVertBuf->Release();
}
};
void mapCube::draw(D3DXMATRIX matWorld)
{
D3DXMATRIX matTranslate;
D3DXMatrixTranslation(&matTranslate,x,y,z); //translation to the cubes stored coordinates
d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate * matWorld)); //...combined with the total world transform
cubeMesh->DrawSubset(0);
};
int checkColl(D3DXVECTOR3 vecTest, D3DXVECTOR3 vecThis) //2nd arg bc compiler decided to forget
{ //that this class has x,y,z vars
if(vecTest.x>=vecThis.x-(TILE_WIDTH/2.0f) || vecTest.x<=vecThis.x+(TILE_WIDTH/2.0f)) //rudimentary attempt at collision checking
{
return 1;
}
else if(vecTest.z>=vecThis.z-(TILE_HEIGHT/2.0f) || vecTest.z<=vecThis.z+(TILE_HEIGHT/2.0f))
{
return 2;
}
else
{
return 0;
}
}
Sorry if the formatting is a bit off, this is the first time I have used this interface. The compiler reports no syntax errors or otherwise, after I change the x/z references in the last function to a passed D3D vector anyway. Any help/criticism is welcome, whether it relates to d3d or c++. I didn't post the main source because I do not believe the problem is there, but if asked I will post it as well.
Following the solution of that problem, I now notice this warning:
1>Debug\mapCube.obj : warning LNK4042: object specified more than once; extras ignored
I removed the duplicate definitions of CUSTOMVERTEX and FVF and made them extern in the header, but am unable to resolve these problems.
Perhaps you forgot to add mapCube.cpp to your VC++ project. Add this line to the top of mapCube.cpp:
#error This file is indeed being compiled
If that line does not generate a compiler error, then mapCube.cpp is not being compiled.
Try Rebuild Solution. Sometimes things get out of sync, and you just have to recompile everything from scratch.
Just thought it is worth pointing to the bleeding obvious, but is mapCube.cpp included in your project?
The second issue is easily solved: the checkColl implementation (in mapCube.cpp) is missing its mapCube:: prefix. This causes the compiler to think it is a "free function", not a member function of mapCube.

Adding dll's to the project and making functions work

I have to do one project and I was given dll and a header file with implemented functions needed for the project. I was told just to add the header file to the project but this way i get unresolve externals error if i try to use functions referenced in header. So what needs to be done to make everything work? Visual Studio 2010.
Here are the files i have: http://www.ipix.lt/images/33871682.png
And this is header file:
#ifndef __BIM482_RADAR__
#define __BIM482_RADAR__
int BIM482OpenRadar();
int BIM482AddPlane(double x, double y);
int BIM482SetPlaneColor(int planeidx, int coloridx);
int BIM482SetPlanePos(int planeidx, double x, double y);
void BIM482UpdateRadar();
#endif // __BIM482_RADAR__
I need to initiate gui with OpenRadar and pass information with those functions. How to start this thing?
You don't have a .lib-file to link against.
Maybe this can help you
http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/
or this:
http://support.microsoft.com/default.aspx?scid=kb;en-us;131313
or this:
http://www.asawicki.info/news_1420_generating_lib_file_for_dll_library.html
when you have generated the lib-file ,you must use __declspec(dllimport) on yuor functions in the header file.
An alternative to all of the above is to use LoadLibrary(...) in your source and write wrapper function for those function in the dll and call those dll funcions via GetProcAddress(...).
struct
{
HMODULE hDll;
int (*BIM482OpenRadar) ();
int (*BIM482AddPlane) (double x, double y);
int (*BIM482SetPlaneColor) (int planeidx, int coloridx);
int (*BIM482SetPlanePos) (int planeidx, double x, double y);
void (*BIM482UpdateRadar) ();
} dll_funcs = {0};
bool ExitRadar( LPCTSTR szDllPath )
{
if (dll_funcs.hDll)
FreeLibrary( dll_funcs.hDll );
return true;
}
bool InitRadar( LPCTSTR szDllPath )
{
if (dll_funcs.hDll)
return true;
dll_funcs.hDll = LoadLibrary( szDllPath );
if (!dll_funcs.hDll)
return false;
dll_funcs.BIM482OpenRadar = (int(*)())GetProcAddress( dll_funcs.hDll ,("BIM482OpenRadar") );
dll_funcs.BIM482AddPlane = (int(*)(double,double))GetProcAddress( dll_funcs.hDll ,("BIM482AddPlane") );
dll_funcs.BIM482SetPlaneColor = (int(*)(int,int))GetProcAddress( dll_funcs.hDll ,("BIM482SetPlaneColor") );
dll_funcs.BIM482SetPlanePos = (int(*)(int,double,double))GetProcAddress( dll_funcs.hDll ,("BIM482SetPlanePos") );
dll_funcs.BIM482UpdateRadar = (void(*)())GetProcAddress( dll_funcs.hDll ,("BIM482UpdateRadar") );
return true;
}
int BIM482OpenRadar ()
{ return (*dll_funcs.BIM482OpenRadar)(); };
int BIM482AddPlane (double x, double y)
{ return (*dll_funcs.BIM482AddPlane)( x ,y ); }
int BIM482SetPlaneColor (int planeidx, int coloridx )
{ return (*dll_funcs.BIM482SetPlaneColor)( planeidx ,coloridx ); }
int BIM482SetPlanePos (int planeidx, double x, double y)
{ return (*dll_funcs.BIM482SetPlanePos)( planeidx ,x ,y ); }
void BIM482UpdateRadar ()
{ return (*dll_funcs.BIM482UpdateRadar)(); }
There is a windows call that loads a DLL into your image. You can either add it to the compile setup in Visual Studio, or load it dynamically.
The easiest thing is probably to do it at compile time; google "Visual Studio llinking options".
You'll need to export the function from dll, while import the function from your exe. Like:
#if defined(XYZLIBRARY_EXPORT) // inside DLL
#define XYZAPI __declspec(dllexport)
#else // outside DLL
#define XYZAPI __declspec(dllimport)
#endif // XYZLIBRARY_EXPORT
int XYZAPI BIM482OpenRadar();
int XYZAPI BIM482AddPlane(double x, double y);
int XYZAPI BIM482SetPlaneColor(int planeidx, int coloridx);
int XYZAPI BIM482SetPlanePos(int planeidx, double x, double y);
void XYZAPI BIM482UpdateRadar();
In DLL, i suggest to add a macro, and add XYZLIBRARY_EXPORT in pre-processor
It will export all of your function.
In EXE, import the function, without adding pre-processor, as it will import the function by default.
In addition to including header files, you need to link your program with the corresponding libraries. From your screenshot, you seem to have .a files, which are indeed libraries, but unfortunately they are meant for use with the GCC toolchain and will not work with Visual Studio. You either need to get .lib files if you must use Visual Studio, or otherwise switch to a GCC port such as MinGW.
You may use windows API LoadLibrary to load the dll during runtime, followed by GetProcAddress to retrieve function address. With the function address is retrieved, you need to declare the method signature so that compiler know how to call given function.
Below is a sample code for how to "link" to function in Dll:
HINSTANCE m_hExtDll = LoadLibrary( "SDL.dll" )
if (m_hExtDll)
{
FARPROC m_pfnOpenRadar = GetProcAddress(m_hExtDll, "BIM482OpenRadar")
if (m_pfnOpenRadar)
{
typedef int (*OPENRADARFUNC)();
OPENRADARFUNC fnOpenRadar = (OPENRADARFUNC)m_pfnOpenRadar;
int iRetCode = fnOpenRadar();
}
}
Do note that functions exported are subject to name mangling and you can verify the function name using third-party utility like Dependency Walker.