How To Use LUA_COMPAT_ALL? - c++

I am new to Lua and especially new to Luabind. When I tried to compile (with Clang++), my first file using Luabind:
#define LUA_COMPAT_ALL
#include <luabind/luabind.hpp>
#include <luaconf.h>
#include <iostream>
int main() {
lua_State *myLuaState = luaL_newstate();
luabind::open(myLuaState);
luaL_dostring(
myLuaState,
"function add(first, second)\n"
" return first + second\n"
"end\n"
);
std::cout << "Result: "
<< luabind::call_function<int>(myLuaState, "add", 2, 3)
<< std::endl;
lua_close(myLuaState);
}
I got a whole heap of error messages.
So, I did a bit of looking around I found it had to do with my Lua version being 5.2 vs 5.1 and found that the solution was LUA_COMPAT_ALL (which I found out at Lua project compiling with errors (luabind)).
Unfortunately, I'm a bit of a scrub when it comes to Lua; so, I don't know where I put that.
I hope my question wasn't too stupid :)

Just defining it in your code should work, as told in the Lua source (CTRL+F for "LUA_COMPAT_ALL"): http://www.lua.org/source/5.2/luaconf.h.html
(It might've been in the manual, but removed from it later on)

Related

getline(ifstream, string) on Mac causes EXC_BAD_ACCESS

I've been a C++ developer since it arrived. All was on windows, and I haven't touched it in about 6 years.
Now I'm trying to get an old code-base working using VS Code on my Mac. I'm using clang++ with c++17.
This problem is vexing; I've seen many other posts with the same issue, but the problem always seemed to be something in the code.
Note: this code worked fine with C++11 on Windows.
To simplify, I copied the code to execute right at the top of main. Here is is:
ifstream file("assets/textures/blocks.txt", ios::in);
if( file.is_open() ) {
string s;
getline(file, s); // <-- This line causes the error.
cout << s << endl;
}
As this code worked elsewhere, I assume I've got a setup or environment problem and am looking for hints towards what to check on.
Thank you for any help!
An update:
Thank you. I paired the program down and tried a few things. Here's the deal:
If I leave all my files to be compiled, but replace main.cpp with the below code, the cout line generates the same exception.
If I cull all the unused files, the code works.
Something in some other file is somehow breaking the stream code. I'm clueless.
#include <iostream>
#include <istream>
using namespace std;
int main() // int argc, char** argv)
{
cout << "Hello World" << endl;
}
I should add: this is a GLFW 3D game engine app. It does not subclass or interact with any stream in any way other than the most basic file read/write operations.

Compiler errors when including setupapi.h in hello world app

I'm trying to debug some C# P/Invoke problem with 32/64 bit using the SetupDiGetDeviceInterfaceDetail function. This involves analysing the SP_DEVICE_INTERFACE_DETAIL_DATA structure. For this, I tried to write a simple C++ program to see the data that's not documented. My C skills aren't sufficient to read the structure sizes from the definitions.
Here's the code for a new C++ Windows console application created with Visual Studio 2017:
#include "pch.h"
#include <iostream>
#include <Setupapi.h>
int main()
{
std::cout << "Size: " << sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) << "\n";
}
As soon as I add the third include, I get 1600+ compiler errors about syntax errors within Microsoft files – missing semicolons, type specifiers and other stuff, spreading over several files like setupapi.h, prsht.h, dpa_dsa.h or commctrl.h. That's well outside my capabilities. Looks like Microsoft has delivered a huge mess. Wondering how Visual Studio was compiled if C++ programming is always like this.
What's wrong here? Why can't I just include that header file as suggested in the documentation?
Is there another way to find out what that sizeof expression would resolve to?
Here's a fixed version:
#include <iostream>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Setupapi.h>
int main()
{
std::cout << "Size: " << sizeof( SP_DEVICE_INTERFACE_DETAIL_DATA ) << "\n";
}
It prints 6, however that's not true, unfortunately for you that structure is variable-length. It's defined like this:
#define ANYSIZE_ARRAY 1
struct SP_DEVICE_INTERFACE_DETAIL_DATA_W
{
DWORD cbSize;
WCHAR DevicePath[ ANYSIZE_ARRAY ];
}
The length varies depending on data.

Simple Regex Usage in C++

I try to do the most basic regex example in C++ using the default lib, and I keep getting either crashes or incoherent behavior.
// with -std=c++11
#include <regex>
using namespace std;
int main()
{
// Copied from the documentation, this one works
if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
std::cout << "string matched\n";
// The most simple example I could try, crash with "regex_error"
if (std::regex_match ("99", std::regex("[0-9]*") ))
std::cout << "integer matched\n";
}
I've tried multiple syntaxes and flags, but nothing seems to work. Since my code seems to be matching all the examples I can find, I'm struggling to see what I'm missing.
As #Wiktor Stribiżew stated, it was just my compiler being too old. Updating the compiler (from gcc 4.1 to gcc 4.9) solved the problem!

Lua Function Not Doing Anything

I finally was able to get Lua running with my compiler using C++. I now tried starting some tutorials and the first program won't do anything. I downloaded the code they used and it still did nothing. No errors however. Test prints out fine but I get no response from the luaL_dostring. I am using v5.1.4-46. Is it possible that i installed it incorrectly in some way that doesn't result in errors? I printed out the memory address of the lua state and that seems to be working. If someone could give me some sample code to run I would really appreciate it.
Here's the code:
#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;
int main()
{
lua_State *L = lua_open();
luaL_openlibs(L);
cout << "Test" << endl;
luaL_dostring(L, "print(\"Hello from Lua!\n\")");
lua_close(L);
return 0;
}
Your dostring syntax generates invalid Lua code... "\n" gets passed to lua parser before execution. you need a "\\n"... think so. So your "luaL_dostring" error code is actually a syntax fault. funny thing...
you should not use dostring for more than testing.

Stringstream not working with doubles when _GLIBCXX_DEBUG enabled

I'm using _GLIBCXX_DEBUG mode to help find errors in my code but I'm having a problem which I think is an error in the library, but hopefully someone can tell me I'm just doing something wrong. Here is a short example which repro's the problem:
#define _GLIBCXX_DEBUG
#include <iostream>
#include <sstream>
int main (int argc, const char * argv[]) {
std::ostringstream ostr;
ostr << 1.2;
std::cout << "Result: " << ostr.str() << std::endl;
return 0;
}
If I comment out the #define then the output is (as expected):
Result: 1.2
With the _GLIBCXX_DEBUG define in place however the output is simply:
Result:
I've tracked this down to the _M_num_put field of the stream being left as NULL, which causes an exception to be thrown (and caught) in the stream and results in no output for the number. _M_num_put is supposed to be a std::num_put from the locale (I don't claim to understand how that's supposed to work, it's just what I've learned in my searching so far).
I'm running this on a Mac with XCode and have tried it with both "LLVM GCC 4.2" and "Apple LLVM Compiler 3.0" as the compiler with the same results.
I'd appreciate any help in solving this. I want to continue to run with _GLIBCXX_DEBUG mode on my code but this is interfering with that.
Someone else has seen this over at cplusplus.com
and here at stackoverflow, too.
Consensus is that it is a known bug in gcc 4.2 for Mac OS, and since that compiler is no longer being updated, it is unlikely to ever be fixed.
Seems to me that you can either (1) use LLVM, or (2) build your own GCC and use it.