I am experiencing an issue with CLang's libastmatchers while working with Postgres sources: it can't find include file. This error is reproduced only when CLangTool is created from two files. If it is created for a separate file, there is no error and function is successfully matched.
Full error:
/home/myuser/postgres/src/conditional.c:1:10: fatal error: 'pg_config_ext.h' file not found
#include "pg_config_ext.h"
^~~~~~~~~~~~~~~~~
1 error generated.
Error while processing /home/myuser/postgres/src/conditional.c.
Directory structure:
postgres/
|-- compile_commands.json
|-- pg_config_ext.h
`-- src
|-- backend
| `-- nodeHash.c
`-- conditional.c
File contents:
nodeHash.c is empty;
conditional.c:
#include "pg_config_ext.h"
pg_config_ext.h:
int f();
compile_commands.json:
[ {
"arguments": [
"clang",
"-c",
"-I..",
"/home/myuser/postgres/src/conditional.c"
],
"directory": "/home/myuser/postgres/src",
"file": "/home/myuser/postgres/src/conditional.c"
},
{
"arguments": [
"clang",
"-c",
"-I../..",
"/home/myuser/postgres/src/backend/nodeHash.c"
],
"directory": "/home/myuser/postgres/src/backend",
"file": "/home/myuser/postgres/src/backend/nodeHash.c"
}
]
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(untitled)
set(CMAKE_CXX_STANDARD 17)
find_package(Clang REQUIRED)
include_directories(${CLANG_INCLUDE_DIRS})
add_definitions(${CLANG_DEFINITIONS})
add_executable(clang_error main.cpp)
target_link_libraries(clang_error PUBLIC clangTooling clangBasic clangASTMatchers)
main.cpp:
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Tooling/Tooling.h>
#include <string>
#include <vector>
#include <memory>
#include <iostream>
using namespace clang::ast_matchers;
using namespace clang;
using namespace clang::tooling;
using namespace std;
string prefix = "/home/myuser/";
string ccPath = prefix + "postgres/";
vector<string> files = {
prefix + "postgres/src/backend/nodeHash.c", // if you comment out this line, there will be no error
prefix + "postgres/src/conditional.c"
};
class Fetcher : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const auto *FS = Result.Nodes.getNodeAs<FunctionDecl>("function")) {
std::cout << "Matched"; // Matches only if one file, not two
}
}
};
int main() {
unique_ptr<clang::tooling::ClangTool> clangTool;
unique_ptr<Fetcher> fetcherInstance;
MatchFinder finder;
static const DeclarationMatcher functionMatcher = functionDecl().bind("function");
string errMsg;
shared_ptr<clang::tooling::CompilationDatabase> cDb = clang::tooling::CompilationDatabase::autoDetectFromDirectory(ccPath, errMsg);
std::cout << errMsg; // No output
clangTool = std::make_unique<ClangTool>(*cDb, files);
fetcherInstance = std::make_unique<Fetcher>();
finder.addMatcher(functionMatcher, fetcherInstance.get());
clangTool->run(newFrontendActionFactory(&finder).get());
}
I am using CLang version 10.0.0
Perhaps, this is the same issue, but there is no answer.
I'm trying to make an audio software (a DAW) using Electron to create the window and c++ to play audio / generate audio / apply audio effects.
I have been searching for a simple, powerful, and cross-platform library to play and process audio, and I've found The Synthesis Toolkit and I'm really happy with it.
Here is the code (it's from the STK demo programs):
#include "BeeThree.h"
#include "RtAudio.h"
using namespace stk;
// The TickData structure holds all the class instances and data that
// are shared by the various processing functions.
struct TickData {
Instrmnt *instrument;
StkFloat frequency;
StkFloat scaler;
long counter;
bool done;
// Default constructor.
TickData()
: instrument(0), scaler(1.0), counter(0), done( false ) {}
};
// This tick() function handles sample computation only. It will be
// called automatically when the system needs a new buffer of audio
// samples.
int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData )
{
TickData *data = (TickData *) userData;
register StkFloat *samples = (StkFloat *) outputBuffer;
for ( unsigned int i=0; i<nBufferFrames; i++ ) {
*samples++ = data->instrument->tick();
if ( ++data->counter % 2000 == 0 ) {
data->scaler += 0.025;
data->instrument->setFrequency( data->frequency * data->scaler );
}
}
if ( data->counter > 80000 )
data->done = true;
return 0;
}
int main()
{
// Set the global sample rate and rawwave path before creating class instances.
Stk::setSampleRate( 44100.0 );
Stk::setRawwavePath("./engine/rawwaves/");
TickData data;
RtAudio dac;
// Figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 1;
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
try {
dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data );
}
catch ( RtAudioError& error ) {
error.printMessage();
goto cleanup;
}
try {
// Define and load the BeeThree instrument
data.instrument = new BeeThree();
}
catch ( StkError & ) {
goto cleanup;
}
data.frequency = 220.0;
data.instrument->noteOn( data.frequency, 0.5 );
try {
dac.startStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
goto cleanup;
}
// Block waiting until callback signals done.
std::cin.get();
data.scaler = 0.025;
std::cin.get();
data.scaler = -1;
std::cin.get();
// Shut down the callback and output stream.
try {
dac.closeStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
}
cleanup:
delete data.instrument;
return 0;
}
I managed to compile this simple demo program with g++, using this command:
g++ -D__LITTLE_ENDIAN__ -D__LINUX_ALSA__ ./engine/engine.cpp -o ./engine/engi -I./engine/include/ -L./engine/lib/ -lstk -lpthread -lasound -lm
But then I try to compile it into an engine.node file with node-gyp, I get this error:
paulux#Paulux-Laptop:~/Documents/Code/FyneWav$ node-gyp build
/usr/bin/ld : can't find -lstk
collect2: error: ld returned 1 exit status
Here's my binding.gyp file:
{
"targets": [
{
"target_name": "engine",
"sources": ["./engine/engine.cpp"],
"cflags_cc" :["-fexceptions"],
"include_dirs": [
"./engine/include/"
],
'link_settings': {
"libraries": [
"-lpthread", "-lasound" , "-lm",
"-L./engine/lib/", "-lstk"
],
},
"defines": [
"__LITTLE_ENDIAN__", "__LINUX_ALSA__"
]
}
]
}
My folders looks like this:
root
|- package-lock.json
|- package.json
|- README.md
|- binding.gyp
|- 10.1.4 (includes for v8 NodeJS addon)
|- engine
|- engine.cpp
|- include (all include files from *STK/include* archive)
|- lib
|- libstk.a (lib from the *STK/src/Release* archive)
I tried not to link stk in the binding.gyp file, but then I loaded the engine.node module in electron, I got:
Uncaught Error: /home/paulux/Documents/Code/FyneWav/build/Release/engine.node:
undefined symbol: _ZN3stk3Stk17sampleRateChangedEdd
So, the question is:
How can I link stk in node-gyp, and, if we can't, how can I make a engine.node file using other compilers (like g++ for example) ?
Finnaly ! I've found by myself !
The answer is really dumb: in my binding.gyp file, I just had to replace
-L./engine/include by
-L/home/paulux/Documents/Code/fynewav/engine/include.
I just had to change from a relative path to an absolute one...
And it took me a day to figure it out...
And I hate myself =)
So lately I've been getting into OpenCV with C++.
I've built up a few libraries and apps that I would like to export over to Nodejs, but I can't figure it out for the life of me.
I tried to check out how he did it in this repo below, but it was a lot to take in especially since this is my first add-on.
https://github.com/peterbraden/node-opencv/blob/master/binding.gyp
I don't mind it being with NAN or N-API, I just am hoping for something simple and easy to see what goes where and why.
Here is a simple OpenCV function that just opens up an image that I am trying to use as an addon with Node:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int ShowImage()
{
String imageName("./image.png");
Mat image;
image = imread(imageName, IMREAD_COLOR);
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", image);
waitKey(0);
}
There are three main files that you will need.
binding.gyp
module.cpp
index.js
binding.gyp
For me The hardest part was figuring out how to include openCV into the project. I don't know if this is correct or not but I looked at the binding.gyp file like a make file in a typical C++ project. With that in mind this is what my binding.gyp file looks like.
{
"targets": [{
"target_name": "module",
'include_dirs': [
'.',
'/user/local/lib',
],
'cflags': [
'-std=c++11',
],
'link_settings': {
'libraries': [
'-L/user/local/lib',
'-lopencv_core',
'-lopencv_imgproc',
'-lopencv_highgui'
],
},
"sources": [ "./src/module.cpp",
"./src/ImageProcessing.cpp" ]
}]
}
The ImageProcessing.cpp file that I wrote needed c++11 so that's why I added that flag it is not necessary to get openCV to work.
The key of the binding.gyp file is the link-settings. This is how you actually include openCV into your project.
Also make sure to include all of your source files in the sources list(I forgot to include my ImageProcessing.cpp file initially)
module.cpp
I used n-api so my module.cpp file looked like this
#include <node_api.h>
#include "ImageProcessing.hpp"
#include "opencv.hpp"
template <typename T>
ostream& operator<<(ostream& output, std::vector<T> const& values)
{
for (auto const& value : values)
{
output << value;
}
return output;
}
napi_value processImages(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 3;
napi_value argv[1];
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
char PathName[100];
size_t result;
status = napi_get_value_string_utf8(env, argv[0], PathName, 100, &result);
char FileName1[100];
status = napi_get_value_string_utf8(env, argv[1], FileName1, 100, &result);
char FileName2[100];
status = napi_get_value_string_utf8(env, argv[2], FileName2, 100, &result);
vector< vector<Point> > Anchors; //to store coordinates of all anchor points
vector< vector<Point> > Regions[4]; //to store coordinates of all corners of all pages
vector<int> Parameters; // image processing parameters
vector<string> FileList1;
vector<string> FileList2;
Mat TemplateROI[NUM_SHEET][4];
Mat Result1, Result2;
string FileName;
string testName = FileName1;
int i;
// The first function to be called only at startup of the program
// provide the path to folder where the data and reference image files are saved
getAnchorRegionRoI(PathName, &Anchors, Regions, &Parameters, TemplateROI);
vector< vector<int> > Answers;
if (Parameters.at(0)) {
namedWindow("Display1", CV_WINDOW_AUTOSIZE);
namedWindow("Display2", CV_WINDOW_AUTOSIZE);
}
napi_value outer;
status = napi_create_array(env, &outer);
//This will need to be changed to watch for new files and then process them
Answers = scanBothSides(FileName1, FileName2, "./Output/", &Result1, &Result2, &Anchors, Regions, Parameters, TemplateROI);
for(int k = 0; k<Answers.size(); k++){
napi_value inner;
status = napi_create_array(env, &inner);
int j;
for(j = 0; j<Answers[k].size(); j++){
napi_value test;
napi_create_int32(env, Answers[k][j], &test);
napi_set_element(env,inner, j, test);
}
napi_value index;
napi_create_int32(env, k, &index);
napi_set_element(env,inner, j, index);
napi_set_element(env,outer, k, inner);
}
if (Parameters.at(0)) {
if (!Result1.empty() && !Result1.empty()) {
FileName = "./Output/" + string("O ") + FileList1[i];
imwrite(FileName, Result1);
FileName = "./Output/" + string("O ") + FileList2[i];
imwrite(FileName, Result2);
resize(Result1, Result1, Size(772, 1000));
resize(Result2, Result2, Size(772, 1000));
imshow("Display1", Result1);
imshow("Display2", Result2);
waitKey(0);
}
}
if (status != napi_ok)
{
napi_throw_error(env, NULL, "Failed to parse arguments");
}
//return PathName;
return outer;
}
napi_value Init(napi_env env, napi_value exports)
{
napi_status status;
napi_value fn;
status = napi_create_function(env, NULL, 0, processImages, NULL, &fn);
if (status != napi_ok)
{
napi_throw_error(env, NULL, "Unable to wrap native function");
}
status = napi_set_named_property(env, exports, "processImages", fn);
if (status != napi_ok)
{
napi_throw_error(env, NULL, "Unable to populate exports");
}
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
This is the file that interfaces with C/C++ and node.
I had trouble with the opencv.hpp file being found so I just moved it into my working directory for now. This is why I used quotes instead of brackets to include it.
Working with the n-api took a little getting used to so make sure you read the docs here
index.js
And finally here is my index.js file
const express = require('express');
const app = express();
const addon = require('./build/Release/module');
const value = "./Data/";
let FileName1 = "./Images/Back1.jpg";
let FileName2 = "./Images/Front1.jpg";
let result = addon.processImages(value, FileName1, FileName2);
console.log("Results: "+result);
server.listen(3000, () => console.log('Example app listening on port 3000!'))
So all you have to do is require your module from the build/Release folder and then call it like any other js function.
Take a look at the module.cpp code again and you will see that in the init function you use the n-api to create a new function. I called mine processImages. This name matches the name of the processImages function at the top of the module.cpp file. Finally in my index.js file I call addon.processImages().
Tips:
I installed node-gyp globally by running npm install -g node-gyp
I compiled my code using the following command: node-gyp configure build
Try getting a simple n-api project working first then add in openCV. I used this tutorial to get started
I created a simple script which will compile OpenCV 3.4 with opencv_contrib (SIFT, SURF available) for statically linking with Native Abstractions for Node.js.
rm -rf 3rdparty/opencv
mkdir -p 3rdparty/opencv
rm -rf tmp
mkdir tmp
cd tmp
rm -rf opencv-master
rm -rf opencv_contrib-master
git clone --branch 3.4 --depth 1 https://github.com/opencv/opencv.git opencv-master
git clone --branch 3.4 --depth 1 https://github.com/opencv/opencv_contrib.git opencv_contrib-master
mkdir build
cd build
cmake \
-DCMAKE_INSTALL_PREFIX="$(pwd)/../../3rdparty/opencv\#3.4" \
-DBUILD_SHARED_LIBS=OFF \
-DENABLE_PRECOMPILED_HEADERS=YES \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules \
../opencv-master
cmake --build .
make install
It will help you to to get started with your computer vision project. By the way, SIFT algorithm is no longer requires a license for use as its patent expired 2020-03-06
{
"targets": [
{
"target_name": "addon",
"cflags": [
"-std=c++11",
"-stdlib=libc++"
],
"cflags_cc!": [
"-fno-rtti",
"-fno-exceptions"
],
"xcode_settings": {
"GCC_ENABLE_CPP_RTTI": "YES",
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
},
"include_dirs": [
"../../3rdparty/opencv/#3.4/include",
"<!(node -e \"require('nan')\")"
],
'libraries': [
"<!#(node utils/find-libs.js)",
"-framework OpenCL"
],
"sources": [
"./src/main.cc",
"./src/lib/MainAddon.cc"
],
}
]
}
The full source code is large enough so It published in this GitHub repository
I am creating a C++ addon and I wanted to use a static library. i have a .a library libarith which does simple addition.
-libarith.a
-hello.cc
-hello.js
my binding.gyp file is as follows:-
{ "targets": [
{
"target_name": "addon",
"sources": [ "hello.cc" ],
"libraries": [ "-/home/folder/api/addon/libarith.a" ],
"cflags!": [ "-fno-exceptions" ],
"cflags": [ "-std=c++11" ],
"cflags_cc!": [ "-fno-exceptions" ]
}
]
}
When i compile my hello.cc it compiles well. But when i run my addon , it gives following error:
node: symbol lookup error: /home/folder/api/addon/build/Release/addon.node: undefined symbol: _ZN4demo3sumEii.
I am new to addons, a help would be very much appreciated.
Code Snippet:-
libarith.a contains:
int sum(int a ,int b){
return a+b;
}
// hello.cc
#include <node.h>
#include <vector>
#include <iostream>
#include <v8.h>
using namespace std;
using namespace v8;
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void newmethod(const FunctionCallbackInfo<Value>& args)
{ extern int sum(int,int);
Isolate* isolate = args.GetIsolate();
double abc= sum(4,5);
Local<Number> newnumber =Number::New(isolate,abc);
v8::String::Utf8Value r(args[1]);
std::string rst(*r);
Local<String> first = String::NewFromUtf8(isolate, "firstargument");
Local<String> second = String::NewFromUtf8(isolate, "secondargument");
Local<Object> newobj= Object::New(isolate);
newobj->Set(first,String::NewFromUtf8(isolate, *s));
newobj->Set(second,newnumber);
args.GetReturnValue().Set(newobj);
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "newmethod", newmethod);
}
NODE_MODULE(addon, init)
}
//hello.js
const addon = require('./build/Release/addon');
var ss = "helloo";
var samplestring = "It is not a sample string";
console.log(addon.newmethod(samplestring, ss));
EDIT:- The solution worked as follows. I tried to create a separate directory for the libraries and it worked fine.
It says, it cannot find the declaration(implementation of .h). I think you give wrong direction to your library. There are two solutions:
Write full directory to your library at binding.gyp ~/whereItIsLocated, in my case it was ~/CLionProjects/node-player-core/PlayerCore/lib/x64/yourLibraryName.a
If previous solution couldn't help, you can copy you library to /usr/lib. You can do it with sudo cp ~/whereLibraryLocated /usr/lib.
Specify the .so library used for appropriate .h file in the binding.gyp file helps me to solve same issue:
"libraries": [
"/usr/lib/mylib.so"
]
This links help me to found it: Getting "Symbol Lookup Error" when calling C library from C++ (Node.js Addon)
I'm having trouble (dynamically) linking with the Qt libraries whilst compiling a C++ project with g++ 4.5.3:
g++ -Wall XMLInOut.cpp tinyxml2.cpp -I"C:\Qt\4.7.1\include" -I"$ADTF_ADDONS\adtf-device-toolbox\include" -L"C:\Qt\4.7.1\lib" -lQtCore4 -DtUInt32=uint -o xmltest.exe
The output is the following :
/tmp/cclJqe9S.o:XMLInOut.cpp:(.text+0x1db): undefined reference to `QString::toUInt(bool*, int) const'
/tmp/cclJqe9S.o:XMLInOut.cpp:(.text$_ZN7QStringC1EPKc[QString::QString(char const*)]+0x15): undefined reference to QString::fromAscii_helper(char const*, int)
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld: /tmp/cclJqe9S.o: bad reloc address 0x15 in section `.text$_ZN7QStringC1EPKc[QString::QString(char const*)]'
If I understand correctly, the -l option will link against a dynamic library. in the Qt/libs I have the QtCore4.dll
What am I doing wrong?
Cheers
EDIT :
Although I believe the problem lies fully in the linkage, I will post my code :
#include "XMLInOut.h"
#include "tinyxml2.h"
#include <iostream>
#include "ModelToInterfaceWrapper.h"
//#define __DEBUG_OUT_
#ifdef __DEBUG_OUT_
#define out(A) cout<<A
#else
#define out(A)
#endif
using namespace std;
using namespace tinyxml2;
void XMLInOut::readAndParseFile(const char* pFilename){
XMLDocument doc;
doc.LoadFile( "config.xml" );
XMLElement *service = doc.FirstChildElement( "root" )->FirstChildElement( "Service" );
for( service; service; service=service->NextSiblingElement() )
{// Iterating Services
out(endl<<service->FirstAttribute()->Value());
QString serviceName;
tUInt32 signalValue ;
const XMLAttribute* description = ((const XMLElement *)service)->FindAttribute("description");
if((const XMLAttribute*)0 != description){
out(" ("<<description->Value()<<")");
serviceName = description->Value();
}else {
serviceName = service->FirstAttribute()->Value();
}
for(XMLElement *signal = service->FirstChildElement( "Signal" ); signal; signal=signal->NextSiblingElement() ){
out(endl<<" "<<signal->GetText());
signalValue = (new QString (signal->GetText()))->toUInt();
}
}
}
int main(int argc, char **argv) {
XMLInOut xmlinOut;
xmlinOut.readAndParseFile("config.xml");
}