Reading from std::cin produces Linker error - c++

I have a rather unusual problem. I am trying to do this:
char *content = new char[10000];
std::cin.read(content, 10000);
And I get the following linker error (weird because the code was compiling fine a few weeks ago, and it hasn't been modified):
Error LNK2001: unresolved external symbol "__declspec(dllimport) public: class std::basic_istream<char,struct std::char_traits<char> > & __thiscall std::basic_istream<char,struct std::char_traits<char> >::read(char *,__int64)" (__imp_?read#?$basic_istream#DU?$char_traits#D#std###std##QAEAAV12#PAD_J#Z) main.obj
I verified that I have all the required dependencies linked in the Project Properties, verified that I have /MT set, and the like. The project was compiling fine just a few weeks ago-- the only thing I have done between then and now is update VS2012. Here are my includes.
#include <stdlib.h>
#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
extern char ** environ;
#endif
#include "fcgio.h"
#include "fcgi_config.h"
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/algorithm/string.hpp>

It appears that after updating Visual Studio 2012, the project (somehow) became non-functional. Copying the exact same code over to a new project has fixed the problem.

Related

LNK2019 when using Lua from NuGet package [duplicate]

I use Lua ver 5.2.3 with visual studio 2010 , when i compile code below
#include "stdafx.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include <stdlib.h>
#include <stdio.h>
void main()
{
lua_State *luaState = luaL_newstate();
}
I got error
error LNK2019: unresolved external symbol "struct lua_State * __cdecl luaL_newstate(void)" (?luaL_newstate##YAPAUlua_State##XZ) referenced in function _wmain
Could you please give me any advice about this .
Thank !!!
This is a result of C++ "name mangling". See how there are strange characters in this term:
?luaL_newstate##YAPAUlua_State##XZ
You have choices:
Change the file extension of this file to .c instead of .cpp, so that it runs through the C compiler instead of the C++ compiler (caveat: depending on compiler)
or
Add extern "C" around the includes like this:
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
or
#include "lua.hpp"
In the last case, that header is included with the 5.2 release. It does what I wrote in option 2 for you.

LNK2001 error on atoi and stoi function with proper includes added

I have this project that throws LNK2001 error on using atoi while proper includes added:
#... //other headers
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <iostream>
BOOL main(int argc, char* argv[]){
ShowNumber(atoi(argv[1]));
return TRUE;
}
Error is: LNK2001: Unresolved external symbol _atoi
I do know that LNK error happens while a header confusion occurs. But i don't think that headers are the problem here. Is there anything in the project configuration or header conflict that may cause this to break? Also i get 15 other LNK2001 on using std::stoi.
Visual studio 2017 with 2015 or 2017 toolset.

trouble when build with boost filesytem

I want to use boost filesystem functions and I searched my build log and find these lines:
1> Searching C:\local\boost_1_55_0\stage\lib\libboost_filesystem-vc90-mt-1_55.lib:
1> Searching C:\local\boost_1_55_0\stage\lib\libboost_system-vc90-mt-1_55.lib:
1> Searching C:\local\boost_1_55_0\stage\lib\boost_system-vc90-1_55.lib:
so it did find the libs.
what I med are:
proc1.obj : error LNK2001: unresolved external symbol "unsigned __int64 __cdecl boost::filesystem::detail::file_size(class boost::filesystem::path const &,class boost::system::error_code *)" (?file_size#detail#filesystem#boost##YA_KAEBVpath#23#PEAVerror_code#system#3##Z)
how to solve this?
my test code:
#include <stdlib.h>
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
using namespace std;
namespace fs = boost::filesystem;
int main()
{
string filepath( "H:\\DataSets\\xxx" );
int a = fs::file_size(filepath.c_str());
}
As you said in your comment, you are only linking with boost::filesystem. boost::filesystem depends on boost::system so you must also link to boost::system

error LNK2019: unresolved external symbol newbie needs tip

I am just probing some more advanced coding in C++, so imagine my frustration when I can't solve this:
1>Main.obj : error LNK2019: unresolved external symbol "int __cdecl playerAtk(void)" (? playerAtk##YAHXZ) referenced in function _main
1>C:\Users\Hezekiah Detinc\documents\visual studio 2010\Projects\Custom_1\Debug\Custom_1.exe : fatal error LNK1120: 1 unresolved externals
I had tried searching for an answer here, but the solutions I have found were too specific to be of any use for me.
If anyone can answer; What generally causes this problem? What are some solutions to fixing it?
This is my Main.cpp;
//Custom 1
//Include files that may be used. will cut out unused files in release.
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include "EnemyBaseClass.h"
#include "PlayerBaseClass.h"
using namespace std;
int main()
{
Enemy emy;
Player plr;
emy.enemyAtk();
emy.enemyDef();
emy.enemyHp();
plr.playerAtk();
plr.playerDef();
plr.playerHp();
int playerAtk();
cout << playerAtk();
cout << "You're attacked by a Monster!\n";
system(" pause ");
return 0;
}
If I need to post my headers or the other _.cpp files, let me know.
You declare and use playerAtk as a function (with no implementation, thus the undefined reference)
Change
int playerAtk();
...
cout << playerAtk();
to
int playerAtk;
...
cout << playerAtk;
(You probably want to initialize the variable)
The error is simple: You are using a function which you have declared, but you have not written any definition (Implementation). Check your source (.cpp) files.

codegen matlab to c++ :- Problems when attempting to build a c++ based .exe rather than c?

I am having a few problems with using codegen (via the gui interface).
I have successfully built a very simple c based .exe program based on the following two files.
coderand.m
function r = coderand() %#codegen
r = rand();
main.c
#include <stdio.h>
#include <stdlib.h>
#include "coderand.h"
int main()
{
printf("coderand=%g\n", coderand());
return 0;
}
If I now try and change out main.c for the same code in a main.cpp,
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "coderand.h"
void main(int argc, char **argv)
{
printf("coderand=%g\n", coderand());
}
I get the following compile errors.
main.obj : error LNK2019: unresolved external symbol "double __cdecl coderand(void)" (?coderand##YANXZ) referenced in function _main 25 F:\CoderTest\coderand.exe : fatal error LNK1120: 1 unresolved externals
Any help much appreciated.
Edit:- Solved by myself...
For those suffering the same problem...
Coder -> More Settings -> All Settings -> Advanced -> Language..change C to C++
C++ can call C functions without difficulty, you just have to let the compiler know that the C calling convention applies to this function, like so:
extern "C" {
# include "coderand.h"
}