I am very new to C++ and I am attempting to setup multiple projects in the same solution in VS2013. Currently I have stepped back to a simpler example project to try to figure out my error.
Project 1:
Main.cpp
#include "Test.h"
#include <iostream>
using namespace std;
int main()
{
cout << _MOVEMENTSPEED();
system("pause");
return 0;
}
Project 2
Test.h
#ifndef TEST_H
#define TEST_H
int _MOVEMENTSPEED();
#endif
Test.cpp
#include "Test.h"
int _MOVEMENTSPEED()
{
return 10;
}
Whenever I attempt to build this I get the error "error LNK2019: unresolved external symbol "int __cdecl _MOVEMENTSPEED(void)" (?_MOVEMENTSPEED##YAHXZ) referenced in function _main c:\Users\Max\documents\visual studio 2013\Projects\Project1\Project2\Main.obj" and "Error 2 error LNK1120: 1 unresolved externals c:\users\max\documents\visual studio 2013\Projects\Project1\Debug\Internal".
UPDATE
I tested this same code but within one project file in visual studio and it worked fine.
When you create multiple projects you should do the following:
Make sure the following:
Include the .h file properly from other project(Generally every project has its own directory), so you need to include the file like below:
#include "..\Test\Test.h"
Export the function / class by using _declspec(dllexport) and _declspec(dllimport)
Include the .lib file properly in the project settings of Link tab.
Set the project dependencies correctly.
The below links should help you:
http://support.microsoft.com/kb/815650
http://msdn.microsoft.com/en-us/library/799kze2z.aspx
Related
I'm trying to use opencv 2.3 with Visual Studio 2010 Express. My code is from example:
#include "stdafx.h"
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
int c;
// allocate memory for an image
IplImage *img;
// capture from video device #1
CvCapture* capture = cvCaptureFromCAM(1);
// create a window to display the images
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
// position the window
cvMoveWindow("mainWin", 5, 5);
while(1)
{
// retrieve the captured frame
img=cvQueryFrame(capture);
// show the image in the window
cvShowImage("mainWin", img );
// wait 10 ms for a key to be pressed
c=cvWaitKey(10);
// escape key terminates program
if(c == 27)
break;
}
return 0;
}
What have I done so far?
Added build\bin and one of build\{x86|x64}\{vc9\vc10\mingw}\bin to my system path (to use DLLs).
Added build\{x86|x64}\{vc9\vc10\mingw}\lib or build\{x86|x64}\{vc9\vc10\mingw}\staticlib as library directories to my linker settings.
Added build\include and build\include\opencv as include directories to my compiler settings.
And the result is:
1>LINK : fatal error LNK1104: cannot open file 'c:\OpenCV2.3\build\x86\vc10\lib.obj'
There's no lib.obj in OpenCV folders. I've only unziped OpenCV-2.3.0-win-superpack.exe, without using CMake software.
What am I doing wrong?
Well, the official guide is for installing OpenCV 2.1 on VS2010, so I wrote some instructions below that shows how to properly install and configure the x86 version of OpenCV 2.3 on Visual Studio 2010 (Express), since a lot of folks seem to have problems setting it up correctly.
Download OpenCV-2.3.0-win-superpack.exe and execute it to extract all files to a folder named OpenCV2.3. Inside this folder there are 2 directories: build and opencv. All the setup on VS2010 will refer to the build directory. For practical purposes I moved the folder OpenCV2.3 to my C:\ drive, so pay attention to the paths I suggest on this guide as yours might be different.
On Visual Studio, create a new Win32 Console Application project and name it whatever you like. After that, a new window will show up. Click on the tab Application Settings and make sure the option Empty Project gets selected:
Add a new file main.cpp to the folder Source Files, then add this code to main.cpp:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: ./opencv_hello <file.png>\n");
return -1;
}
IplImage* img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (!img)
{
return -1;
}
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
cvShowImage("display", img );
cvWaitKey(0);
return 0;
}
At this point, we need to configure the project so it can locate OpenCV headers and libraries. Go to the Project Properties (ALT+F7), and once the new window shows up do the following:
On the Configuration box, select All Configurations
Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directories to add these 3 paths (for the headers):
C:\OpenCV2.3\build\include\opencv
C:\OpenCV2.3\build\include\opencv2
C:\OpenCV2.3\build\include
Note that include\opencv is for the C interface of OpenCV and include\opencv2 if for the C++ interface. We are also adding the folder include to prevent our build from being broken by some headers of the C interface that refer to C++ headers as opencv2\core.
Then, add the path of the libraries on Configuration Properties > Linker > General, and on the Additional Library Directories field, add this: C:\OpenCV2.3\build\x86\vc9\lib:
Finally, for this simple test we are going to add the libraries opencv_core230.lib and opencv_highgui230.lib. So go to Configuration Properties > Linker > Input and add them:
When writing more complex applications you'll probably need to add other OpenCV libs that I did not
mentioned on this little project of ours.
Press F7 to Build Solution and you should see:
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
To be able to execute the application you'll need to modify the PATH environment variable of your system to add the location of OpenCV's DLLs. Add this to end of PATH:
; C:\OpenCV2.3\build\x86\vc9\bin
If you are struggling with editing the PATH environment variables, you can also copy the required .dll files to your project folder:
The dll files are located in this folder ../OpenCV2.3/build.x86/vc9/bin
Then copy them to the folder where .exe file is created:
c:\Users\PIMMES\Documents\Visual Studio 2010\Projects\eigenfaces\Debug (Ofcourse you have to change the path to your Debug folder)
You only have to copy the .dll files which you are using in your project (#include for example) For example if you get an error message saying opencv_core231d.dll is not found then get this .dll file from the above location (bin folder) and copy to your project Debug folder.
Hope this helps..
Whenever I make a program that uses opencv 2.2 or greater I include everything, and then comment out the libraries I don't need. Try this, I'm sure you need more than highgui.h
#include "opencv2\opencv.hpp"
using namespace cv;
//#pragma comment(lib, "opencv/opencv_calib3d231.lib")
//#pragma comment(lib, "opencv/opencv_contrib231.lib")
#pragma comment(lib, "opencv/opencv_core231.lib")
//#pragma comment(lib, "opencv/opencv_features2d231.lib")
//#pragma comment(lib, "opencv/opencv_flann231.lib")
//#pragma comment(lib, "opencv/opencv_gpu231.lib")
//#pragma comment(lib, "opencv/opencv_haartraining_engine.lib")
#pragma comment(lib, "opencv/opencv_highgui231.lib")
//#pragma comment(lib, "opencv/opencv_imgproc231.lib")
//#pragma comment(lib, "opencv/opencv_legacy231.lib")
//#pragma comment(lib, "opencv/opencv_ml231.lib")
#pragma comment(lib, "opencv/opencv_objdetect231.lib")
//#pragma comment(lib, "opencv/opencv_ts231.lib")
//#pragma comment(lib, "opencv/opencv_video231.lib")
I'm trying to use glload from the Unofficial OpenGL SDK but I get LNK errors:
1> LINK : C:\Users\T\documents\visual studio 2010\Projects\testi\Debug\testi.exe not found or not built by the last incremental link; performing full link
1>glloadD.lib(gll_gl_ext.obj) : error LNK2019: unresolved external symbol __imp__wglGetProcAddress#4 referenced in function _WinGetProcAddress
1>glloadD.lib(wgll_ext.obj) : error LNK2001: unresolved external symbol __imp__wglGetProcAddress#4
1>C:\Users\T\documents\visual studio 2010\Projects\testi\Debug\testi.exe : fatal error LNK1120: 1 unresolved externals
#include <glload/gl_all.h>
#include <glload/gll.hpp>
void main()
{
glload::LoadFunctions();
}
Linker/Additional Dependencies: glloadD.lib
Where is the problem ?
Edit 1:
First I used Premake to generate build files for vs2010. Then I built all libraries. In my project I set Additional Include Directories, Additional Library Directories and Additional Dependencies for those libraries. I want to run an example from this page: link but I forgot to create OpenGL context before loading opengl functions. I don't need a window in this project so I just call glutInit, but I get an unhandled exception at 0x5bfed398 (msvcr100d.dll)
#include <glload/gl_all.h>
#include <glload/gll.hpp>
#include <freeglut/freeglut.h>
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glload::LoadFunctions();
}
Edit 2:
Calling glutCreateWindow before glload::LoadFunctions seems to be necessary. Following code works:
#include <glload/gl_all.h>
#include <glload/gll.hpp>
#include <freeglut/freeglut.h>
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutCreateWindow("");
glload::LoadFunctions();
}
The solution is to create a working context before initializing GlLoad. I've had this problem every time i've started an OpenGL project with GLSDK.
I am trying to wrap a C++ class "OpenViBE::Kernel::CPlayer" into a Managed C++ application, so I can later use it in C#.
#include "stdafx.h"
#include "ovkCPlayer.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
const OpenViBE::Kernel::IKernelContext* r=nullptr;
OpenViBE::Kernel::CPlayer* c=new OpenViBE::Kernel::CPlayer(*r);
//c->initialize();
return 0;
}
The above code compiles, it but does not link. The "OpenViBE::Kernel::CPlayer" is in project "OpenViBE-kernel-dynamic". I found where is the lib file from Properties->Linker->Advanced->Import Library. Then I added this folder to the lib path of my project (above) and the "OpenViBE-kernel-dynamic.lib" to the Linker->Input->Additional dependencies.
So the lib file is there, but the linker still can not link it:
error LNK2019: unresolved external symbol "public: __thiscall OpenViBE::Kernel::CPlayer::CPlayer(class OpenViBE::Kernel::IKernelContext const &)" (??0CPlayer#Kernel#OpenViBE##$$FQAE#ABVIKernelContext#12##Z) referenced in function ...
I do not have experience in writing C++/CLI applications, so I think I am missing something.
I'm trying to compile this code:
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <luabind/luabind.hpp>
#include<iostream>
int main(){
lua_State*pL=lua_open();
luabind::open(pL);
lua_close(pL);
return 0;
}
But I don't have a .lib of luabind, so I use the source with the .h/.cpp files.
The way I do it is by adding the directories to include, but I get a link error.
The only way I can compile is by adding the .cpp files as existing elements, but the solution tree gets messy with the additional files.
Can somebody tell me if there's a way to add the directory of the additional .cpp files in the solution's properties?
Thanks
Compile the lua cpp files into a static library. Add the directory where you put those under "linker | input | additional library directories".
You need to tell the linker where to find the functions referenced by the .h files (the .lib file, typically).
I used to work with math.h without any problem. Now, I use an external library which itself has a file called math.h, but which includes < cmath>.
Adding this library to my project (or even just adding the include directory, without touching the code) now generates tons of errors from < cmath> :
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2039: 'acosf' : is not a member of '`global namespace''
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2873: 'acosf' : symbol cannot be used in a using-declaration
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2039: 'asinf' : is not a member of '`global namespace''
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2873: 'asinf' : symbol cannot be used in a using-declaration
[etc, etc...]
I don't understand why this happens. I am using Visual Studio 2005 and looking on the internet, it seems that this problem is solved under VS 2008. However, I'd like to stay on VS 2005...
Including using namespace std; everywhere, or changing the order of my includes doesn't seem to change anything. Defining _STD_BEGIN solves the error, but produce as many in < xlocinfo>.
How can this be solved?
Same Problem exists in VC 10. I think, that <cmath> includes itself a math.h but insted of the correct one, which is shipped with VC it uses the one which is created in the User-Project (with different content of course).
Solution: Do never use a File named math.h in your Project... (Or correct the std somewhere).
I'm not sure I read your question correctly but it seems odd that a library would ship it's own math.h file.
Perhaps you are suppose to put the parent directory in your include path so that <my_lib/math.h> can be included without conflicting with your compiler <math.h>?
The problem is probably mixing C libraries with C++ conventions. For instance:
#include <math.h>
namespace TEST {
}
This compiles fine, whereas:
namespace TEST {
#include <math.h>
}
This generates a large number of spurious errors.
Just to confuse the issue:
#include <math.h>
namespace TEST {
#include <math.h>
}
This also compiles as it can only be included once (the first time).
Hence also:
#include <math.h>
namespace TEST {
#include "SomethingThatIncludesMath.h"
}
Will work, whereas:
namespace TEST {
#include "SomethingThatIncludesMath.h"
}
Won't.
You can also get similar problems by including C++ headers into a *.c file, rather than a *.cpp file.
I am sure that other similar mixing of C and C++ can lead to similar problems.
(1) According to Microsoft, the C2873 means;
'symbol' : symbol cannot be used in a using-declaration
A using directive is missing a namespace keyword. This causes the compiler to misinterpret the code as a using declaration rather than a using directive.
(2) Also when I had C2873 with C2039 (I tried to merge CEF3 and Cinder), somehow I bypassed the both error by changing Properties->Configuration Properties->C/C++->Code Generation;
Enable Minimal Rebuild: Yes(/Gm), Enable C++ Exception: Yes(/EHsc), Enable Function-Level Linking: empty