LNK2019 when using Lua from NuGet package [duplicate] - c++

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.

Related

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.

Reading from std::cin produces Linker error

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.

Using .c and .cpp files in Visual Studio at the same time

Trying to figure out how to get an application to compile that uses both C and C++ files. Not the entire code, but enough to get the idea:
main.cpp:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "one.h"
#include "two.h"
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hInst2, LPSTR lpCmdLine, int nShowCmd) {
FunctionOne();
FunctionTwo();
}
one.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <gdiplus.h>
#include <gdiplusflat.h>
using namespace Gdiplus;
using namespace Gdiplus::DllExports;
int FunctionOne() {
}
two.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int FunctionTwo() {
}
The header files contain only definitions for those functions.
Now, if I compile this with a main.cpp, I get an "unresolved external symbol" for FunctionTwo. If I compile this with a main.c, I get the same thing for FunctionOne. Is this even possible, and if so, how would I set up the project to compile properly (Visual Studio 2010)?
It compiles fine if I comment out the alternate function depending on the extension for main.
Thanks!
The problem is two.h, it almost certainly wasn't written to allow a C++ compiler to properly compile the C function prototype. You'll want to take advantage of the predefined __cplusplus macro, like this:
two.h:
#ifdef __cplusplus
extern "C" {
#endif
int FunctionTwo();
// etc...
#ifdef __cplusplus
}
#endif
Lovely macro soup ;) If the header is pre-baked and never saw a C++ compiler before then do this in your .cpp source code file:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "one.h"
extern "C" {
#include "two.h"
}
Some programmers name their header files .hpp if they contain C++ declarations and .h if they contain C declarations. That's a pretty good practice I personally favor. So does the Boost team. It didn't otherwise set the world on fire.
C++ does name-mangling to support function overloading while C does not. You will have to mark your function as extern "C" to prevent name mangling.
// main.cpp
extern "C" int FunctionTwo();
.. the rest ..
// two.c
extern "C" int FunctionTwo() {
// stuff
}
See http://www.parashift.com/c++-faq/mixing-c-and-cpp.html for more information on mixing C and C++.

"unresolved external symbol _triangulate" when using triangle library

I'm currently using the triangle library in my program. The library contains only .c and .h files (no .lib). I get the following error on Visual Studio C++ 2010:
1>data.obj : error LNK2019: unresolved external symbol _triangulate referenced in function "struct triangulateio __cdecl readfile(void)" (?readfile##YA?AUtriangulateio##XZ)
The header file of my data.cpp is the following:
#ifndef DATA_H
#define DATA_H
#include <WinSock2.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <time.h>
#include <GL/gl.h> //include the gl header file
#include <GL/glut.h> //include the glut header file
#include <GL/glu.h> //include the glut header file
#include <armadillo>
//Namespace
using namespace std;
using namespace arma;
extern "C"
{
#ifdef SINGLE
#define REAL float
#else /* not SINGLE */
#define REAL double
#endif /* not SINGLE */
#include "triangle.h"
}
triangulateio readfile();
#endif
Data.cpp
triangulate("pczAevn", &in, &mid, &vorout);
I've already made my program work with a Makefile of mine on Ubuntu, but I need to run my program on windows.
Feel free to ask for more information.
EDIT #1:
If you use the triangle library with VS, you have to put the following instruction on top of the triangle.c file #define TRILIBRARY
Now it compile. Thank you very much for the help.
The linker can't find a definition for "triangulateio readfile()", if it's defined in the .c file my guess is that it isn't built. If you include it in the project it could work.

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"
}