Require Lua Module In Script Called from C++ - c++

I'm using VS2015 in a C++ application with Lua5.1. I'm running a very simple Lua script with no issue, raw lua works fine. but when I attempt to import a lua module "socket.http" my application doesn't like it because I imagine it can't find the module.
My question is how do I allow my lua script (being run from c++) to access lua modules like socket.http?
My project.cpp
#include "stdafx.h"
#include <iostream>
extern "C"
{
#include <../dependancies/lua51/include/lua.h>
#include <../dependancies/lua51/include/lauxlib.h>
#include <../dependancies/lua51/include/lualib.h>
}
void report_errors(lua_State *L, int status)
{
if (status != 0)
{
printf("-- %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // remove error message
}
}
int main()
{
// create a Lua state
lua_State* L = luaL_newstate();
// load standard libs
luaL_openlibs(L);
int lscript = luaL_dofile(L, "test1.lua");
report_errors(L, lscript);
system("PAUSE");
return 0;
}
test1.lua
local http = require("socket.http")
errors
module 'socket.http' not found:
no field package.preload['socket.http']
no file '.\socket\http.lua'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\lua\socket\http.lua'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\lua\socket\http\init.lua'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket\http.lua'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket\http\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\socket\http.luac'
no file '.\socket\http.dll'
no file '.\socket\http51.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket\http.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket\http51.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\socket\http.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\socket\http51.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\loadall.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\loadall.dll'
no file '.\socket.dll'
no file '.\socket51.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket51.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\socket.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\socket51.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\loadall.dll'
no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\loadall.dll'

Rules for modules is the same, no matter if you had script started from c++ or from a Lua command line interpreter.
You must have that module in the path where Lua searchers/loaders will try to find it. See the list of paths searched, put that http dll (compiled with same settings as your project, in case Lua is linked statically) in one of searched paths.
And you have to distribute that module along with your program, don't expect it to be installed on user's pc.

Related

Not able to include standard c++ library headers in vs code esp-idf extension

I am not able to include some standard libraries:
#include <stdio.h> // <= this works
// #include <thread> // <= error: "No such file or directory"
// #include <algorithm> // <= error: "No such file or directory"
void app_main(void)
{
}
The error is generated by compiler xtensa-esp32-elf-gcc.exe. There is no error from C/C++ Intellisense. And I am able to find the necessary header by pressing F12. They are placed on the path C:\Espressif\tools\xtensa-esp32-elf\esp-2021r2-patch3-8.4.0\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\8.4.0. There is no such path in the compiler -I arguments.
How can I deal with this error?
Thread and algorithm are both part of C++ standard library, not C standard library. I've renamed the source file to .cpp (CMakeLists.txt was updated by VSCode automatically). Then I added:
extern "C" {
void app_main();
}
And it works. Hope it helps somebody.

Visual Studio c++ no symbols loaded for dll after moving dll into proper code path

I am trying to create a simple application in Visual Studio 2019 that connects to SQL Server. I am using the sqlapi++ library to create the connection. I am trying to gain more experience with c++ and 3rd party libraries. My experience in c++ has mostly been on a macbook with xcode and standard c++ libraries.
I get the following error:
then I get this error:
No symbol loaded for sqlapi.dll
binary was not built in with debug information.
It specifies the file location as C:\Users\name\source\repos\ProjectName\Debug\sqlapi.dll. I look in this file location and it is there. Which is also where the exe file gets generated. My code so far is very simple:
#include <SQLAPI.h>
#include <iostream>
int main()
{
SAConnection con;
SACommand cmd(&con);
try
{
con.Connect(
"Database#ServerName",
"Username",
"Password",
"SA_SQLServer_Client);
cmd.setCommandText("SELECT * FROM dbo.TableName");
cmd.Execute();
}
catch (int exception)
{
}
}
I set a breakpoint on con.Connect() and continue, then it loads symbols for a few seconds before giving me an error. My question is, how can I ensure that my .dll files get loaded in Visual Studio properly? Should my .dll and .pdb files all be moved to the file location that the .exe files gets generated in? Or just put them in the same file as the .cpp file?

I can't run basic C++ program " Hello World " in Visual Studio

I'm learning C++ as first programming language and I can't figure out how to run Hello World program. I was googling for solution but I did not find any.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Unable to start program
'C:\Users/thom/Desktop/C++/Visual/firstproject/Debug/Hello World.exe'.
The system cannot find the file specified.
If you create a .cpp file in File > New > File > C++ File?
If so, the path is not included in the project directory, causing the compiler to fail to find the file.
The correct way is to create a cpp file in the project directory under Explorer.As shown below:
You can modify the name of the .cpp, and don't modify the default location.
Now you can try to build and run the executable.
Try Build -> Rebuild Solution, find the executable in the path shown at the output to run it.
You can also try pressing Ctrl+F5 to build and run

Qt with Lua | Where to place lua file(s)

I've created a cpp file with the main method implementing calls to lua.
When compiling + executing the qt project I receive the following error:
PANIC: unprotected error in call to Lua API (attempt to call a string value)
The problem is that lua cannot find the lua file to be executed (at least I think it is). So I copied the file to all the debug dirs and the main dir but it still didn't work.
Thanks for helping me!
main.cpp
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
lua_State* L;
int main(int argc, char *argv[])
{
/* initialize Lua */
L = luaL_newstate();
/* load Lua base libraries */
luaL_openlibs(L);
/* load the script */
luaL_loadfile(L, "test.lua");
lua_call(L, 0, 0);
/* cleanup Lua */
lua_close(L);
}
and the file test.lua
-- test
print("Hello World")
Given that this is a run-time file operation, the function is likely only looking in the current directory.
It seems like Qt uses the user's directory as default path.
You can validate this with: (thanks Rich)
QDir dir(".");
qDebug() << dir.absolutePath();
When putting the lua script into this folder everything works like a charm.
The working directory can be set with the following command:
QDir::setCurrent()
And in combination with
QCoreApplication::applicationFilePath()
The path can be set to where the exe resigns in.
This is not really related to Qt since you are not using any of the Qt API in your software.
You should check the status returned by the lual_loadfile method like shown in this example. That should give you some additional clues about what is going wrong.
Just in case, there's a QtLua module that could be of interest.

Error trying to run a file being read in C++

I am trying to read a file so that I can average out the numbers listed in the file. I believe my code is correct, but I keep getting an error in Visual Studio stating, "Unable to start program ... The system cannot find the file specified." The file I want to read, "numbers.dat" is in the directory, but it still shows this error.
I'm new to C++ so I was wondering if anyone would be able to help?
Here is my code
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream myfile;
myfile.open("numbers.dat");
int total = 0, count = 0, num;
while (!myfile.eof()){
myfile>>num;
total += num;
count++;
}
cout<<"The "<<count<<" numbers total "<<total<<" and average "<<total/count<<endl;
myfile.close();
system("pause");
return 0;
}
Spanky is most likely correct, but to provide more information -
Visual Studio compiles your program into a Build Directory. When you run your program through Visual Studio it runs from the Build Directory. By default the Build Directory is not the same folder as your source code, so your program won't find files in that are mixed in with your source code.
Possible solutions:
You can copy the file into your Build Directory
You can change your Build Directory
You can use absolute or relative file paths to point to the correct location
I don't use MSS for development, but it's a usual practice that people miss a thing that a directory, from where the program is launched, and the directory you think the program is launched, are different.
For example, u've got a build directory
d:\project\build,
Compiled binary (.exe) is located in
d:\project\build\debug directory.
Well, your numbers.dat should probably be located in d:\project\build directory and NOT in d:\project\build\debug.