in c++, luabridge::LuaRef:no appropriate default constructor available - c++

I write a lua script and want to parse the script by c++ codes. In this script I have functions, and I want to get the function and save it for usage in the future. The main.cpp is like
#include <string>
#include <iostream>
#include "luaParser.h"
#include "LuaBridge.h"
extern "C" {
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}
using namespace luabridge;
void main (void)
{
lua_State* L = luaL_newstate();
if (luaL_dofile(L, "P3626_PORT.lua"))
{
printf("%s\n", lua_tostring(L, -1));
}
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
LuaParser parser;
parser.luaParse(L); // get some values from the luaParser::luaParse function
lua_close(L);
parser.run(); // call the run function defined in LuaParser class
parser.stop(); // call the stop function defined in LuaParser class
}
The P3626_PORT.lua is like:
lua_name = "P3626_PORT.lua"
run = function()
print (" this is my input!!!!!!!")
end
stop = function()
print (" this is my output!!!!!!!")
end
The luaParser.h is like:
#pragma once
#include <string>
#include "LuaBridge.h"
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
using namespace luabridge;
class LuaParser
{
public:
LuaParser();
virtual ~LuaParser();
void luaParse(lua_State* L); // in this function, run (pcall) the script and retrieve functions
void run();
void stop();
private:
LuaRef mRun;
LuaRef mStop;
};
And at last, luaParser.cpp is like this:
#ifdef _WIN32
#pragma warning(disable: 4786)
#endif
#include <stdlib.h>
#include <assert.h>
#include "LuaParser.h"
#include <iostream>
#include <string>
LuaParser::LuaParser(){}
LuaParser::~LuaParser(){}
void LuaParser::luaParse(lua_State* L)
{
using namespace luabridge;
LuaRef serviceName = getGlobal(L, "service_name");
std::string LuaServiceName = serviceName.cast<std::string>();
std::cout << LuaServiceName << std::endl;
// now Let's read the function
mRun = getGlobal(L, "run");
mStop = getGlobal(L, "stop");
}
void LuaParser::run()
{
mRun();
}
void LuaParser::stop()
{
mStop();
}
When compiling the project, I get the error
error c2512: 'luabridge::LuaRef':no appropriate default constructor available
I have tried to solve this problem by such as initializing list but it doesn't work. Any idea how to solve this problem?

Related

CompileLayerT::addModule crashed for no reason

I'm trying to create a llvm JIT in my code:
#include <iostream>
#include <vector>
#include <memory>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Verifier.h"
#include "KaleidoscopeJIT.h"
#include "llvm/Support/TargetSelect.h"
int main() {
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
llvm::LLVMContext context;
llvm::orc::KaleidoscopeJIT jit;
auto module = std::make_unique<llvm::Module>("top", context);
llvm::IRBuilder<> builder(context);
module->setDataLayout(jit.getTargetMachine().createDataLayout());
llvm::FunctionType *funcType = llvm::FunctionType::get(builder.getVoidTy(), false);
llvm::Function *mainFunc =
llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", module.get());
llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "", mainFunc);
builder.SetInsertPoint(entry);
module->print(llvm::errs(), nullptr);
auto H = jit.addModule(std::move(module));
...
...
}
when comes to the addModule, the program crashed(interrupted by signal 11: SIGSEGV), I debugged the stack:
llvm::Instruction::getNumSuccessors() const 0x0000565102dbd7e0
llvm::BranchProbabilityInfo::updatePostDominatedByUnreachable(llvm::BasicBlock const*) 0x000056510361d797
llvm::BranchProbabilityInfo::calculate(llvm::Function const&, llvm::LoopInfo const&, llvm::TargetLibraryInfo const*) 0x00005651036220ef
llvm::BranchProbabilityInfoWrapperPass::runOnFunction(llvm::Function&) 0x00005651036225bd
llvm::FPPassManager::runOnFunction(llvm::Function&) 0x0000565102ddc640
llvm::FPPassManager::runOnModule(llvm::Module&) 0x0000565102ddc8b3
llvm::legacy::PassManagerImpl::run(llvm::Module&) 0x0000565102ddcc7e
llvm::orc::SimpleCompiler::operator() CompileUtils.h:68
llvm::orc::LegacyIRCompileLayer<llvm::orc::LegacyRTDyldObjectLinkingLayer, llvm::orc::SimpleCompiler>::addModule IRCompileLayer.h:84
llvm::orc::KaleidoscopeJIT::addModule KaleidoscopeJIT.h:66
main main.cpp:34
__libc_start_main 0x00007f8d8ee38b97
_start 0x0000565102cdc1ca
The KaleidoscopeJIT.h:
//===- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Contains a simple JIT definition for use in the kaleidoscope tutorials.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include <algorithm>
#include <map>
#include <memory>
#include <string>
#include <vector>
namespace llvm {
namespace orc {
class KaleidoscopeJIT {
public:
using ObjLayerT = LegacyRTDyldObjectLinkingLayer;
using CompileLayerT = LegacyIRCompileLayer<ObjLayerT, SimpleCompiler>;
KaleidoscopeJIT()
: Resolver(createLegacyLookupResolver(
ES,
[this](const std::string &Name) {
return ObjectLayer.findSymbol(Name, true);
},
[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
ObjectLayer(ES,
[this](VModuleKey) {
return ObjLayerT::Resources{
std::make_shared<SectionMemoryManager>(), Resolver};
}),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}
TargetMachine &getTargetMachine() { return *TM; }
VModuleKey addModule(std::unique_ptr<Module> M) {
auto K = ES.allocateVModule();
cantFail(CompileLayer.addModule(K, std::move(M)));
ModuleKeys.push_back(K);
return K;
}
void removeModule(VModuleKey K) {
ModuleKeys.erase(find(ModuleKeys, K));
cantFail(CompileLayer.removeModule(K));
}
JITSymbol findSymbol(const std::string Name) {
return findMangledSymbol(mangle(Name));
}
private:
std::string mangle(const std::string &Name) {
std::string MangledName;
{
raw_string_ostream MangledNameStream(MangledName);
Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
}
return MangledName;
}
JITSymbol findMangledSymbol(const std::string &Name) {
#ifdef _WIN32
// The symbol lookup of ObjectLinkingLayer uses the SymbolRef::SF_Exported
// flag to decide whether a symbol will be visible or not, when we call
// IRCompileLayer::findSymbolIn with ExportedSymbolsOnly set to true.
//
// But for Windows COFF objects, this flag is currently never set.
// For a potential solution see: https://reviews.llvm.org/rL258665
// For now, we allow non-exported symbols on Windows as a workaround.
const bool ExportedSymbolsOnly = false;
#else
const bool ExportedSymbolsOnly = true;
#endif
// Search modules in reverse order: from last added to first added.
// This is the opposite of the usual search order for dlsym, but makes more
// sense in a REPL where we want to bind to the newest available definition.
for (auto H : make_range(ModuleKeys.rbegin(), ModuleKeys.rend()))
if (auto Sym = CompileLayer.findSymbolIn(H, Name, ExportedSymbolsOnly))
return Sym;
// If we can't find the symbol in the JIT, try looking in the host process.
if (auto SymAddr = RTDyldMemoryManager::getSymbolAddressInProcess(Name))
return JITSymbol(SymAddr, JITSymbolFlags::Exported);
#ifdef _WIN32
// For Windows retry without "_" at beginning, as RTDyldMemoryManager uses
// GetProcAddress and standard libraries like msvcrt.dll use names
// with and without "_" (for example "_itoa" but "sin").
if (Name.length() > 2 && Name[0] == '_')
if (auto SymAddr =
RTDyldMemoryManager::getSymbolAddressInProcess(Name.substr(1)))
return JITSymbol(SymAddr, JITSymbolFlags::Exported);
#endif
return nullptr;
}
ExecutionSession ES;
std::shared_ptr<SymbolResolver> Resolver;
std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
ObjLayerT ObjectLayer;
CompileLayerT CompileLayer;
std::vector<VModuleKey> ModuleKeys;
};
} // end namespace orc
} // end namespace llvm
#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
But if I run codes in https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.html, it works well, so I guess the environment is right.
Any idea why it crashed?
AlexDenisov's comment is right: BasicBlocks should end with terminator instructions. Try adding
builder.CreateRetVoid();
to your code between the call to builder.setInsertPoint and module->print.

segfault at the end of the main()

I am compiling a program agains LLVM-CLANG. This is the main
#include <iostream>
#include "CompilerFactory.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/Diagnostic.h"
int main(int argc, char const *argv[])
{
using clang::CompilerInstance;
using clang::TargetOptions;
using clang::TargetInfo;
using clang::FileEntry;
using clang::Token;
using clang::DiagnosticOptions;
using clang::TextDiagnosticPrinter;
CompilerInstance ci;
CSFV::CompilerFactory::GetCompilerInstance(ci);
const FileEntry *pFile = ci.getFileManager().getFile("test.c");
ci.getSourceManager().createMainFileID(pFile);
ci.getPreprocessor().EnterMainSourceFile();
ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(),
&ci.getPreprocessor());
Token tok;
do
{
ci.getPreprocessor().Lex(tok);
if (ci.getDiagnostics().hasErrorOccurred())
break;
ci.getPreprocessor().DumpToken(tok);
std::cerr << std::endl;
} while (tok.isNot(clang::tok::eof));
ci.getDiagnosticClient().EndSourceFile();
return 0;
}
and this is the included class
//If they are not defined we have an error at compile time
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#include "llvm/Support/Host.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
using namespace clang;
namespace CSFV{
class CompilerFactory
{
public:
CompilerFactory();
~CompilerFactory();
/// \brief Generate and returns a compiler instance object
static void GetCompilerInstance(CompilerInstance &ci){
DiagnosticOptions diagOpts;
TextDiagnosticPrinter* diagPrinter =
new TextDiagnosticPrinter(llvm::outs(), &diagOpts, true);
ci.createDiagnostics(diagPrinter);
llvm::IntrusiveRefCntPtr<TargetOptions> pto (new TargetOptions());
pto->Triple = llvm::sys::getDefaultTargetTriple();
TargetInfo *pti =
TargetInfo::CreateTargetInfo(ci.getDiagnostics(), pto.getPtr());
ci.setTarget(pti);
ci.createFileManager();
ci.createSourceManager(ci.getFileManager());
ci.createPreprocessor();
return;
}
};
} //end of namespace CSFV
For some reason I get a segfault at the end of the execution of the main. What am I missing?
I don't know if this is the same problem I had, but what I did was declare the targetoptions as a new pointer.
eg:
clang::TargetOptions *targetOpts=new clang::TargetOptions;
I have a feeling targetinfo cleans it up when itself is destroyed.

vector<wstring> as Return value and Parameter

I want to create some modules for my program. I want to call a function and pass a vector as a parameter. The return value should also be a vector.
My code looks like this
main.cpp
//BlueSmart.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
using namespace std;
#pragma comment(lib, "Irprops.lib")
BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {
sizeof(BLUETOOTH_FIND_RADIO_PARAMS)
};
BLUETOOTH_RADIO_INFO m_bt_info = {
sizeof(BLUETOOTH_RADIO_INFO),
0,
};
BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
1,
0,
1,
1,
1,
15,
NULL
};
BLUETOOTH_DEVICE_INFO m_device_info = {
sizeof(BLUETOOTH_DEVICE_INFO),
0,
};
HANDLE m_radio = NULL;
HBLUETOOTH_RADIO_FIND m_bt = NULL;
HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;
int wmain(int argc, wchar_t **args) {
while(true) {
m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio);
do {
localBluetoothDevices ();
m_search_params.hRadio = m_radio;
::ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);
vector<wstring> vec;
int m_device_id = 0;
do {
wostringstream tmp;
++m_device_id;
//Something like this <----------------------------------------
externBluetoothDevices (vec);
//Something like this <----------------------------------------
wprintf(L"********************************************************************** \n");
wprintf(L"\tDevice %d:\r\n", m_device_id);
wprintf(L"\t\tName: %s\r\n", m_device_info.szName);
wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]);
wprintf(L"====================================================================== \n");
for (int i = 0; i < 6; i++) {
tmp << hex << m_device_info.Address.rgBytes [i];
if (i < 5)
tmp << L':';
}
vec.push_back(tmp.str());
} while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));
BluetoothFindDeviceClose(m_bt_dev);
//Sleep(10*1000*60);
Sleep(10000);
} while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));
BluetoothFindRadioClose(m_bt);
}
return 0;
}
//Lokal verfügbare bzw. angeschlossene Bluetooth-Devices
void localBluetoothDevices (){
int m_radio_id = 0;
m_radio_id++;
BluetoothGetRadioInfo(m_radio, &m_bt_info);
//Lokaler Bluetoothadapter
wprintf(L"====================================================================== \n");
wprintf(L"Local Device Nr. %d\n", m_radio_id);
wprintf(L"\tName: %s\r\n", m_bt_info.szName);
wprintf(L"\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_bt_info.address.rgBytes[0], m_bt_info.address.rgBytes[1], m_bt_info.address.rgBytes[2], m_bt_info.address.rgBytes[3], m_bt_info.address.rgBytes[4], m_bt_info.address.rgBytes[5]);
}
//Extern verfügbare bzw. Bluetooth-Devices
vector<wstring> externBluetoothDevices (vector<wstring> &vec){
return vec;
}
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <bthdef.h>
#include <BluetoothAPIs.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iomanip>
#include <conio.h>
void localBluetoothDevices ();
vector<wstring> externBluetoothDevices (vector<wstring>);
It says that vector is not a known type. What am I doing wrong?
In stdafx.h replace
vector<wstring> externBluetoothDevices (vector<wstring>);
with
std::vector<std::wstring> externBluetoothDevices (std::vector<std::wstring>);
Basically the issue was although you put using namespace std; in your cpp file that doesn't count in your header file which is before the using declaration is seen.
Also note that your defintion in the cpp file is different. In the cpp file you have a reference
vector<wstring> externBluetoothDevices (vector<wstring>&);
Decide which you really want.
You should pass a pointer of a vector.

OpenKinect c++ - Freenect::Freenect freenect

I have a problem with the code OpenKinect provides. I'm trying to use the Kinect with c++, but I get this error. On the web this question was already asked, but I have not find a decent answer. The code is this:
#include <cstdlib>
#include "libfreenect.h"
#include "libfreenect.hpp"
#include <pthread.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <cmath>
#include <vector>
#if defined(__APPLE__)
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
using namespace std;
class Mutex {
...
};
class MyFreenectDevice : public Freenect::FreenectDevice {
public:
MyFreenectDevice(freenect_context *_ctx, int _index)
: Freenect::FreenectDevice(_ctx, _index),
m_buffer_depth(freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes),
m_buffer_video(freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes),
m_gamma(2048),
m_new_rgb_frame(false),
m_new_depth_frame(false)
{
for (unsigned int i = 0; i < 2048; i++) {
float v = i / 2048.0;
v = std::pow(v, 3) * 6;
m_gamma[i] = v * 6 * 256;
}
}
...
private:
std::vector<uint8_t> m_buffer_depth;
std::vector<uint8_t> m_buffer_video;
std::vector<uint16_t> m_gamma;
Mutex m_rgb_mutex;
Mutex m_depth_mutex;
bool m_new_rgb_frame;
bool m_new_depth_frame;
};
Freenect::Freenect freenect;
MyFreenectDevice* device;
freenect_video_format requested_format(FREENECT_VIDEO_RGB);
...
I get the error Unable to resolve identifier identifier freenect for the instruction Freenect::Freenect freenect;.
The code of this template is in the libfreenect.hpp code:
class Freenect : Noncopyable {
private:
typedef std::map<int, FreenectDevice*> DeviceMap;
public:
...
template <typename ConcreteDevice>
ConcreteDevice& createDevice(int _index) {
DeviceMap::iterator it = m_devices.find(_index);
if (it != m_devices.end()) delete it->second;
ConcreteDevice * device = new ConcreteDevice(m_ctx, _index);
m_devices.insert(std::make_pair<int, FreenectDevice*>(_index, device));
return *device;
}
...
}
I have no idea of what is the problem, this is the official code and it should work.. any suggestions? thanks in advance

Duplicate Symbol XCode g++-4.2

OKay, so I'm trying to set some global variables that can be accessed by the rest of my program by including a header file. However, XCode is telling me that I have duplicate symbols. Can anyone help?
Error: Duplicate symbol _ArrowKey in /Path/to/MKDBControlInterface.o /Path/to/main.o
main.h: // The global variables to be accessed...
#ifndef _main_h
#define _main_h
#include <map>
std::map<int,bool> ArrowKey;
#endif
MKDBControlInterface.h:
#ifndef _MKDBControlInterface_h
#define _MKDBControlInterface_h
#include <map>
#include <GLUT/glut.h>
#include "main.h"
#include "MKDBApplication.h"
class MKDBControlInterface {
public:
MKDBControlInterface( MKDBApplication& App )
: m_App( App )
{
glutSpecialFunc( SpecialListener );
glutSpecialUpFunc( SpecialListenerX );
ArrowKey[GLUT_KEY_LEFT] = false;
ArrowKey[GLUT_KEY_RIGHT] = false;
ArrowKey[GLUT_KEY_UP] = false;
ArrowKey[GLUT_KEY_DOWN] = false;
}
~MKDBControlInterface(){}
void static SpecialListener( int key, int x, int y ){
ArrowKey[key] = true;
}
void static SpecialListenerX( int key, int x, int y ){
ArrowKey[key] = false;
}
private:
MKDBApplication& m_App;
};
#endif
main.cpp
#include "main.h"
#include "MKDBApplication.h"
#include "MKDBControlInterface.h"
#include "MKDBRender.h"
int main( int argc, char *argv[] ){
MKDBApplication App;
MKDBControlInterface Interface( App );
MKDBRender Render( App );
return 0;
}
In main.h you need to declare ArrowKey as
extern "C" std::map<int,bool> ArrowKey;
and in main.cpp after the includes you should define it:
std::map<int,bool> ArrowKey;
BTW, I would also replace #ifndef/#define/#endif with #pragma once in the headers.