Clang Source to Source Transformation - clang

I am working on some Clang source-to-source transformation. I am doing it from Clang source code: clang/tools/extra. What I do is that I am adding a printf("Hello World\n"); at the beginning of the main method. It works perfectly, I run my tool as bin/add-code ../../test/hello.c and it turns:
#include <stdio.h>
int main(){
printf("This is from main ....\n");
return 0;
}
to this:
#include <stdio.h>
int main(){
printf("Hello World\n");
printf("This is from main ....\n");
return 0;
}
add-code is my clang libtool that I have written.
But this rewritter only write changes to the terminal; while I want to compile hello.c with the modification and want to do it with clang command, clang -c hello.c not like I have done here.
How could I do that?

Related

Using OMPL with Drake

I am trying to work with ompl on drake and am having issues with using the ompl headers in drake. I installed ompl using the default installation path and made the following changes in drake:
Added the following to the workspace:
new_local_repository(
name = "ompl",
path = "/usr/local/include/ompl-1.5/ompl",
build_file = "ompl.BUILD",
)
ompl.BUILD:
cc_library(
name = "ompl",
hdrs = glob(["**"]),
includes = ["include"],
visibility = ["//visibility:public"],
linkstatic = 1,
)
In my repository's BUILD.bazel:
drake_cc_binary(
name = "ompl_ex",
srcs = ["src/ompl_ex.cc"],
data = [],
test_rule_args = ["--target_realtime_rate=0.0"],
deps = [
"#gflags",
"#ompl//:ompl",
],
)
ompl_ex.cc
#include <memory>
#include <limits.h>
#include <unistd.h>
#include <fstream>
#include <string>
#include <gflags/gflags.h>
#include <iostream>
#include <ompl/config.h>
#include <vector>
namespace drake {
namespace ompl {
int DoMain(){
std::cout<<"the function is working"<<std::endl;
return 0;
}
} // namespace examples
} // namespace drake
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
return drake::ompl::DoMain();
}
The error I get:
infinite_horizon_ltl/src/ompl_ex.cc:10:10: fatal error: ompl/config.h: No such file or directory
#include <ompl/config.h>
I use spot libraries similarly and they seem to be working fine. Not sure what I am getting wrong in the case of OMPL. I checked on drakes git issues and found that there was an attempt at integration but the branch is deleted now, and the solution proposed does not seem to be working on my system.
I have found that it is easiest to work with drake as an external library in a cmake project. This works quite well with ompl. Here's a quick example which imports both drake and ompl as external libraries and solves a simple planning problem.
https://github.com/DexaiRobotics/drake-torch/tree/master/examples/drake-ompl
The output and video were made using the dexai2/drake-torch/cpu-nightly-ros docker which is available here: https://hub.docker.com/r/dexai2/drake-torch/tags
As an aside, depending on which version of Ubuntu you use, you may need change your gcc version - e.g. you need gcc-9 and g++-9 specified in the CMakeLists.txt for Ubuntu 20.04 and gcc-7 and g++-7 on 18.04. See notes here: https://drake.mit.edu/developers.html#id10

undefined reference to `pow' in contiki application

Is it possible to work with math.h library in contiki-cooja simulator ?.
I am using contiki 3.0 on ubuntu 18.04 LTS
I tried adding LDFLAGS += -lm in makefile of hello-world application. Moreover, i also tried adding -lm in Makefile.include file. Things don't work. What is the correct place to add -lm.
hello-world.c
#include "contiki.h"
#include <stdio.h> /* For printf() /
#include <math.h>
#define DEBUG DEBUG_PRINT
static float i;
/---------------------------------------------------------------------------/
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
/---------------------------------------------------------------------------/
PROCESS_THREAD(hello_world_process, ev, data)
{
PROCESS_BEGIN();
i = 2.1;
printf("Hello, world\n");
printf("%i\n", (int)pow(10,i));
printf("%i\n", (int)(M_LOG2Ei));
PROCESS_END();
}
/---------------------------------------------------------------------------/
Makefile
CONTIKI_PROJECT = hello-world
all: $(CONTIKI_PROJECT)
CONTIKI = ../..
include $(CONTIKI)/Makefile.include
LDFLAGS += -lm
First, you can added external libraries to Contiki with:
TARGET_LIBFILES = -lm
Make sure you do this before the include $(CONTIKI)/Makefile.include line, not after!
Second, which platform are you compiling for? The msp430 platforms do not have the pow function in the math library. They only have the powf function operating on single-precision floating point numbers, and the built-in (intrinsic) function pow operating on integers.
If you want to operate on floating point numbers, change your code to this:
float f = 2.1;
pow(10, f);
to this
float f = 2.1;
powf(10, f);

Using Clang to get AST

I would like to use Clang to get to its AST to get some information about the variables and methods in a particular source file. However, I do not want to use the LibTooling facilities. I would like to, by hand, to write to code that would call the methods to parse the .cpp and then get the tree. I can not find any resources that tell me how to do this. Can anybody help?
If your goal is to learn how to drive Clang components to work with the compilation database, configure the compiler instance, and so on, then the Clang source code is a resource. Perhaps the source for the ClangTool::buildASTs() method would be a good place to start: see Tooling.cpp in the lib/Tooling/ directory of the source tree.
If your goal is to do an analysis that LibTooling doesn't support, and you just want to get the AST with minimal fuss, then either ClangTool::buildASTs or clang::tooling::buildASTFromCode might be of service. The ClangTool approach would be better if you need the compilation database to express compiler options, include paths, and so on. buildASTFromCode is fine if you have a standalone bit of code for lightweight tests. Here's an example of the ClangTool approach:
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include <memory>
#include <vector>
static llvm::cl::OptionCategory MyOpts("Ignored");
int main(int argc, const char ** argv)
{
using namespace clang;
using namespace clang::tooling;
CommonOptionsParser opt_prs(argc, argv, MyOpts);
ClangTool tool(opt_prs.getCompilations(), opt_prs.getSourcePathList());
using ast_vec_t = std::vector<std::unique_ptr<ASTUnit>>;
ast_vec_t asts;
tool.buildASTs(asts);
// now you the AST for each translation unit
...
Here's an example of buildASTFromCode:
#include "clang/Frontend/ASTUnit.h"
#include "clang/Tooling/Tooling.h"
...
std::string code = "struct A{public: int i;}; void f(A & a}{}";
std::unique_ptr<clang::ASTUnit> ast(clang::tooling::buildASTFromCode(code));
// now you have the AST for the code snippet
clang::ASTContext * pctx = &(ast->getASTContext());
clang::TranslationUnitDecl * decl = pctx->getTranslationUnitDecl();
...

Opencv linking libraries

i have installed opencv 3.1.0 and i want to compile following program:
#include <stdio.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
int main(void){
return 0;
}
but it returns error:undefined reference to cvRound and so on in header file types_c.h.
I know it must be a linking problem but the only libraries i can link are:
opencv_world310.lib, opencv_world310d.lib
I have already tried to link these but that doesn't solve the problem.
I m using a GNU GCC Compiler and dynamic libraries a link with mingw32-g++.exe.
According to some research in the internet i need to link libraries like:
`opencv_calib3d249d.lib`
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_ml249d.lib
opencv_nonfree249d.lib
opencv_objdetect249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_videostab249d.lib`
but those are not in my lib directory!

fatal error LNK1104: cannot open file 'opencv_calib3d246.dll'

I'm trying to run visual c++ with OpenCV. I have linked the OpenCV to Visual studio 2012. when I tried to run the code, it's giving me an error;
LINK : fatal error LNK1104: cannot open file 'opencv_calib2d246.dll'
Here's what I was trying to do:
#include "stdafx.h"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
if(argc !=2)
{
cout <<"usage: display_image ImageToLoadAndDisplay"<<endl;
return -1;
}
Mat image;
image=imread(argv[1],CV_LOAD_IMAGE_UNCHANGED);
if(! image.data)
{
cout<<"couldn't open or find the image"<<endl;
return -1;
}
namedWindow("Display Window",WINDOW_AUTOSIZE);
imshow("Display Window",image);
waitKey(0);
return 0;
}
I have included all the libraries. I'm using OpenCV 2.4.6, on windows 7 32 bit system.
Anything more I have to add, or do I have to initialize it in the program?
Update
The path for OpenCV in my hard disk :E:\opencv\opencv. Path in the system environment variable: %OPENCV_DIR%\x86\vc11\bin;, where I have created a new variable as OPENCV_DIR and have given the path as E:\opencv\opencv\build. And in linker\command line
/OUT:"E:\VS2012 Projects\cvtest\Debug\cvtest.exe" /MANIFEST /NXCOMPAT /PDB:"E:\VS2012 Projects\cvtest\Debug\cvtest.pdb" /DYNAMICBASE "opencv_calib3d248.lib" "opencv_calib3d248d.lib" "opencv_contrib248.lib" "opencv_contrib248d.lib" "opencv_core248.lib" "opencv_core248d.lib" "opencv_features2d248.lib" "opencv_features2d248d.lib" "opencv_flann248.lib" "opencv_flann248d.lib" "opencv_gpu248.lib" "opencv_gpu248d.lib" "opencv_highgui248.lib" "opencv_highgui248d.lib" "opencv_imgproc248.lib" "opencv_imgproc248d.lib" "opencv_legacy248.lib" "opencv_legacy248d.lib" "opencv_ml248.lib" "opencv_ml248d.lib" "opencv_nonfree248.lib" "opencv_nonfree248d.lib" "opencv_objdetect248.lib" "opencv_objdetect248d.lib" "opencv_ocl248.lib" "opencv_ocl248d.lib" "opencv_photo248.lib" "opencv_photo248d.lib" "opencv_stitching248.lib" "opencv_stitching248d.lib" "opencv_superres248.lib" "opencv_superres248d.lib" "opencv_ts248.lib" "opencv_ts248d.lib" "opencv_video248.lib" "opencv_video248d.lib" "opencv_videostab248.lib" "opencv_videostab248d.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X86 /INCREMENTAL /PGD:"E:\VS2012 Projects\cvtest\Debug\cvtest.pgd" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Debug\cvtest.exe.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /TLBID:1
Now I'm not able to load image. No fatal errors and nothing. It is considering the if statement and not loading anything.
Any suggestions?
You need to set up more than just your linker dependencies and it is very likely you have missed a step.
I would suggest following this tutorial as it will get you setup completely.

Resources