LNK2019 Despite the contents of code (Visual Studio 2013, C++) - c++

I have recently updated to Visual Studio 2013, and to avoid the known problems with uninstalling one of the two programs (I previously used 2010) while the other is installed I uninstalled 2010 before I installed 2013.
Despite what code I place in it, even simple code that is only a few lines, Everything gets this LNK2019 error.
#include<iostream>
using namespace std;
int main()
{
cout << "Testing" << endl;
system("pause");
return 0;
}
Before anyone comments with the "Do not use system("pause")" I know this and I did it purely for simplicity reasons to see if the code would even compile as it would not with any of my longer code either.
Here is a copy of the Error message.
Error 1 error LNK2019: unresolved external symbol WinMain#16 referenced in >function __tmainCRTStartup C:\Users\z49203\Documents\Visual Studio >2013\Projects\JCCNEW\JCCNEW\MSVCRTD.lib(crtexew.obj) JCCNEW
As for extensions in Visual Studio, I have the English version of it with the Japanese language pack. As I need to be able to code using both character sets.

Make your project a Console application not a Window Form one. Otherwise the program needs a WinMain as entry point instead of main.

Related

Including files from a seperate project in the same solution in Visual studio - LNK2001?

I had a solution named fun.sln with a project called fun.vcxproj.
I created a whole bunch of name spaces ready to be used.
I made another project called no_more_fun.vcxproj.
I added the includes directory for fun.vcxproj to the configuration of no_more_fun.vcxproj.
I added this to no_more_fun.cpp
#include "candy.h"
void main(void)
{
candy::get();
return;
}
candy.h is in the default directory for fun.vcxproj(which was added to the config)
But I get...
LNK2001 unresolved external symbol "int __cdecl candy::get(unsigned long)" (?get#candy##YAHK#Z) .....
Visual Studio shows no error before compiling.
The "candy" namespace works fine in the "fun" project so idn...
Is there a guide or something so that i can understand how i can go about sharing code efficiently among different projects within ONE solution?
This is a linker error. You didn't get any error at compile time, because the compiler found the candy::get() method in candy.h header, but the implementation (which I suppose is in a candy.cpp file) is not found by the linker.
Just add candy.cpp too to no_more_fun.vcxproj.
ps. I didn't observe but in the error message you can see that the function waits a parameter.
call it like this:
unsigned long foo = 0;
candy::get(foo);
This is going to sounds stupid but...i just dragged the files in to visual studio so that the no_more_fun project had the "files" in its "directory" too.
wow... I shouldn't need to do that...Am I wrong?(sarcasm).

Unresolved external symbol __report_rangecheckfailure

I have this simple program which creates a prompt for password. I have both Visual Studio 2012 and 2010 installed. It works on 2010, but on 2012 I get this error: unresolved external symbol __report_rangecheckfailure referenced in function main. Also, I modified the code by moving everything from main to another function. That works on both versions.
There must be some problems with some library, but which one and how do I solve it? Thanks.
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int key, i;
char pwd[64];
printf("Enter passphrase: ");
key=getch();
i=0;
while (key!=13)
{
cout<<"*";
pwd[i++]=key;
key=getch();
}
pwd[i++]='\0';
cout<<strlen(pwd)<<endl;
for (i=0; i<strlen(pwd); i++)
cout<<pwd[i]<<endl;
getch();
}
If the project was started in VS 2010, then VS will use MS VS 10.0 C libs. If you start it with 2012, it will use vers. 11.0 libs. VS 2012 will call __report_rangecheckfailure when compiling, but it is not in the same place in version 10 as it is in 11. You can update your project to VS 2012 in the main project menu, or disable the buffer security check.
Further Reading on VS /gs compiler option

Global variables in C++ and CriticalSections

I have several global critical sections that need visibility across 3 or more classes defined in 3 or more .cpp files. They're defined in an h file as:
GlobalCS.h
#pragma once
#include "stdafx.h"
extern CRITICAL_SECTION g_cs1;
extern CRITICAL_SECTION g_cs2;
etc...
In a GlobalCS.cpp they are defined
#include "stdafx.h"
#include "GlobalCS.h"
CRITICAL_SECTION g_cs1;
CRITICAL_SECTION g_cs2;
Then in the cpp for the classes they need to work in they're included as
#include "stdafx.h"
#include "GlobalCS.h"
The linker is complaining giving unresolved external in the files where the critical sections are being used. I didn't expect this since the variables are defined as extern. What am I doing wrong or how do I get around this?
error LNK2001: unresolved external symbol "struct _RTL_CRITICAL_SECTION g_CS_SymbolStrPlotAccess" (?g_CS_SymbolStrPlotAccess##3U_RTL_CRITICAL_SECTION##A)
Not sure why you are getting the error. I tried the same thing in Visual Studio and in _tmain function I wrote the following:
int _tmain(int argc, _TCHAR* argv[])
{
//::g_cs1;
ZeroMemory(&g_cs1, sizeof(::g_cs1));
return 0;
}
And it built with no issues whatsoever.
Thank you all for your help. It always is helpful to have sanity checks. The issue was once again Visual Studio setup. Visual Studio will generate link errors if it doesn't like the way you have files added to your project. This is the second bug I've encountered that generated a link error based on the way the project was configured. If it's that important VS should either prevent you from modifying a project in a harmful way or provide a proper error message.
Anyway, the error is the same as this one:
LNK2019 Error under Visual Studio 2010
I had the GlobalCS.h and GlobalCS.cpp in the source directory. I prefer it this way because I find it makes finding files and code faster and in a large c++ project, just moving around the code base is a significant time waster. So much time could be saved writing c++ code if the IDE was designed to help find code faster. 2012 is A LOT better than 2010 so I'll give MSFT that but there could be a lot more features to that end (afterall VS has been around for almost 2 decades now)these types of persistent problems just get in the way of development. When I moved GlobalCS.h to the Header folder and cleaned the project and rebuilt, everything compiled as expected. The other similar error VS will throw at you is when the .h file is in the code directory (so #includes work) but not in the project. I got the same error messages when that happened and it took a good couple days to figure that one out. In a small project, it might not be as problematic but in big solution with multiple projects and dozens of files, it can be problematic.

xDispatch LNK2019 Linker error Unresolved Externals

I realise this i mighty popular question, but all of the other posts seem to follow the same idea. That the include, lib and bin folder need to be correctly configured. I believe my settings are correct, but i am still getting the LNK2019 error. What am i doing wrong?
Using Visual Studio 2012
I am getting the infamous LNK2019 error. Many posts of this issue seem to think it is a linker issue. I am using a library with lib and dll files. The project folder has an include, bin and lib folder that need to be configured in the project.
Install instructions are here.....
http://opensource.mlba-team.de/xdispatch/docs/current/tutorial.html
However, this is clearly a linker issue as it occurs in other libraries of a similar type. I have followed the instructions for other posts and i'm still a bit lost. I believe this should be correct, but i have tried virtually every possible combination. Don't get it.
My Project Configurations
C++ -> General -> Additional Include Directories.
C:\Users\Daniel\Documents\Visual Studio 2012\Projects\LibDispatchTest\xdispatch_0.7.2_Visual Studio 10_i386\include;%(AdditionalIncludeDirectories)
Linker -> General -> Additional Library Directories
C:\Users\Daniel\Documents\Visual Studio 2012\Projects\LibDispatchTest\xdispatch_0.7.2_Visual Studio 10_i386\lib;%(AdditionalLibraryDirectories)
Linker -> Input -> Additional Dependancies:
.....uuid.lib;odbc32.lib;odbccp32.lib;xdispatch.lib;%(AdditionalDependencies)
Environment Variables.
I have the PATH variable set.
C:\Chocolatey\bin;C:\Users\Daniel\Documents\Visual Studio 2012\Projects\VisionBase\xdispatch_0.7.2_Visual Studio 10_i386\bin;
The error messages are:
error LNK2019: unresolved external symbol "_declspec(dllimport) public: void __thiscall xdispatch::queue::async(class std::function<void __cdecl(void)> const &)" (__imp?async#queue#xdispatch##QAEXABV?$function#$$A6AXXZ#std###Z) referenced in function "void __cdecl some_function(void)" (?some_function##YAXXZ) c:\Users\Daniel\documents\visual studio 2012\Projects\LibDispatchTest\LibDispatchTest\main.obj LibDispatchTest
error LNK1120: 1 unresolved externals c:\users\daniel\documents\visual studio 2012\Projects\LibDispatchTest\Debug\LibDispatchTest.exe 1 1 LibDispatchTest
Seriously, am totally lost and i do not see what i am doing wrong here.
EDIT 1
This is similar to a sample from the above link, but modified to just couNT 1000000^2 and print some stuff. We just want to be able to compile and run this sample and i can correct my larger project exhibiting this issue. This project was made fresh with the simplest code that is representative of what i need to get working. Both this sample and my other project have this problem and produce the same error.
#include <xdispatch/dispatch>
#include <vector>
#include <cmath>
class SomeData {
public:
std::vector<double> a;
std::vector<double> b;
std::vector<double> c;
std::vector<double> results;
};
void do_calculations(SomeData* sd){
// our output will go in here
sd->results = std::vector<double>(sd->a.size());
// the calculation - running on one thread only
for(unsigned int i = 0; i < 1000000; i++){
sd->results[i] = 0;
for(unsigned int j = 0; j < 10000000; j++){
for(unsigned int z = 0; z < sd->c.size(); z++){
std::cout << i << " " << j << std::endl;
}
}
} }
/* This function is getting called from your main thread also powering the user interface */
void some_function() {
SomeData* sd = new SomeData();
xdispatch::global_queue().async(${
// execute the heavy code
do_calculations(sd);
}); }
int main() {
some_function();
return 0; }
Is anyone able to help?
The binaries you used where built with and for MS Visual Studio 2010. Since then the CRT has changed quite a bit and might not be compatible, especially when parts of the STL is used. Because of that I do not recommend to ever use a C++ dll built with VS 2010 for use in VS 2012 or later. It could be that building and linking succeeds but during runtime you experience weird and unexpected issues. In your case even linking won’t work because std::function has changed its signature in the meantime, so lucky enough you already discover those incompatibilities at compile time.
I assume xdispatch will work just fine with VS2012 and maybe even VS2013, however you will need to do your own build for that. All information needed is summarized at [1]. A tarball containing the sources can always be found at [2].
[1] http://opensource.mlba-team.de/xdispatch/docs/current/requirements.html
[2] http://opensource.mlba-team.de/xdispatch/files/

Having trouble embedding Lua for Windows install into C++ program

This is the first question I have found myself not being able to get to the bottom of using my normal googling/stack overflowing/youtubing routine.
I am trying to compile a minimal Lua program inside of a C++ environment just to ensure my environment is ready to development. The Lua language will be later used for User Interface programming for my C++ game.
First some basic information on my environment:
Windows 7 64-bit
Visual studio 2010
Lua for Windows 5.1 (latest build I could download from google code)
Here is the code I am trying to compile:
// UserInt.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"lua5.1.dll")
#include "stdafx.h"
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * ls = luaL_newstate();
return 0;
}
#endif // __LUA_INC_H__
Here is the Error I am getting:
1>UserInt.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _wmain
1>c:\users\deank\documents\visual studio 2010\Projects\UserInt\Debug\UserInt.exe : fatal error LNK1120: 1 unresolved externals
Things I have tried:
I have read about lua_open()(and several other functions) no longer being used so I tried the newstate function instead. I get the same error. This was more of a sanity check than anything. I am using 5.1 and not 5.2 so I do not think this really matters.
I have also read this thread Cannot link a minimal Lua program but it does not seem to help me because I am not running the same environment as that OP. I am on a simple windows 7 and visual studio environment.
The top pragma comment line was something I saw in yet another thread. I get the same error with or without it.
I have gone into my visual studio C++ directories area and added the lua include to the includes and the lua lib to the libraries.
So it seems like my program is seeing the .h and seeing the symbol. But for some reason it is not getting the .cpp implementation for the functions. This is why I was hoping including that .dll directly would help fix the problem, but it hasn't.
So, I feel like I have exhausted all of my options solving this on my own. I hope someone is able to help me move forward here. Lua looks like an awesome language to script in and I would like to get my environment squared away for development.
I hope it is just some silly error on my part. I believe I have provided as much information as I can. If you need more specifics I will update with info if I can provide it.
Edit1
Tried the solution in this Can't build lua a project with lua in VS2010, library issue suspected
That did not work either.
You'll need to have the library (.LIB) file and add that to VS. Use Project > Properties and go to Linker > Input and add the full .lib filename to the "Additional Dependencies" line. Note that the .LIB is different from the .DLL.
Personally, I prefer adding the source code to my project, over referencing the dynamic link library. The following procedure will let you do as such.
Download the source code ( http://www.lua.org/ftp/ ), uncompress it.
In Visual Studio, choose File > New > Project and choose Visual C++, Win32, "Win32 Console Application".
In your project in Visual Studio, add all the source code, except luac.c. Also delete the main() function out of the file that VS created for you. This is usually given the name of the project you specified with the .cpp file extension. You could just remove this file all-together from the project.
Build and Run.
This is the Lua console