Error in adding TargetData in PassManager in llvm - llvm

I am beginner to llvm. I am implementing a program main.cpp of https://github.com/davidar/lljvm/blob/master/backend/main.cpp . When I am declaring a PassManager PM
PM.add(new DataLayout td), it throws a error :
error: missing template arguments before ‘PM’
PassManager PM;
As keeping above error in mind I change the command as
PassManger<Module> PM
PM.add(new DataLayout td)
Now, there is different error : error: ‘class llvm::PassManager<llvm::Module>’ has no member named ‘add’ I dont know whether the above changes is correct way or not?
Here is a source code:
#include "backened.h"
#include <iostream>
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/PassManager.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/DataLayout.h"
#include <llvm/Transforms/Scalar.h>
#include "llvm/IR/LegacyPassManager.h"
using namespace llvm;
using namespace std;
static cl::opt<string> input(cl::Positional, cl::desc("Bitcode File.."),cl::Required);
static cl::opt<string> classname("classname",cl::desc("Binary name of the generated class..."));
int main(int argc, char** argv)
{
cl::ParseCommandLineOptions(argc, argv, "LLVM Bitcode ..");
LLVMContext context;
ErrorOr<unique_ptr<MemoryBuffer>> mb = MemoryBuffer::getFile(input);
if(error_code ec = mb.getError())
{
errs() << ec.message();
return 1;
}
Expected<unique_ptr<Module>> m = parseBitcodeFile(mb->get()->getMemBufferRef(),context);
if(error_code ec= errorToErrorCode(m.takeError()) )
{
PassManager<Module> PM;
DataLayout td("e-p:32:32:32"
"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64"
"-f32:32:32-f64:64:64");
PM.add(new DataLayout(td));
return 0;
}
Also, suggest me whether I have implemented a correct way the DataLayout td inplace of TargetData td as I found TargetData.h is removed from newer version of llvm.

The PassManager used in that example has moved into the legacy namespace,
so use #include "llvm/IR/LegacyPassManager.h"
Change the variable declaration to legacy::PassManager PM;

Related

Undefined reference to class from namespace in C++

I'm new to C++ and trying to do a small quant project with paper trading.
I have a header file alpaca/client.h as follows:
#pragma once
#include <iostream>
#include <../utils/httplib.h>
#include <config.h>
using namespace std;
namespace alpaca {
class Client {
private:
alpaca::Config* config;
public:
Client();
string connect();
};
}
The implementation in alpaca/client.cpp is
#include <iostream>
#include <string>
#include <client.h>
#include <httplib.h>
using namespace std;
namespace alpaca {
Client::Client() {
config = &alpaca::Config();
};
string Client::connect() {
httplib::Client client(config->get_url(MARKET));
auto res = client.Get("/v2/account");
if (res) {
return res->body;
}
else {
return "Error in Client::get_account(): " + to_string(res->status);
}
};
}
And my main.cpp is:
#include <iostream>
#include <string>
#include <client.h>
using namespace std;
int main()
{
alpaca::Client client = alpaca::Client();
client.connect();
return 0;
}
However, I see the following error when I try to compile with g++:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\shubh\AppData\Local\Temp\cc765kwL.o:main.cpp:(.text+0x1ca): undefined reference to 'alpaca::Client::Client()'
Could anyone help with what exactly I'm missing? I'm not too sure.
The g++ command I use is g++ -I./src/alpaca src/main.cpp
It looks like you forgot to compile the client.cpp file. The error message is saying that the linker cannot find a definition for the Client class constructor.
Try compiling both main.cpp and client.cpp with the g++ command, like this:
g++ -I./src/alpaca src/main.cpp src/alpaca/client.cpp

Compiler can't find DPI related functions

I included all the needed headers (windows.h, winuser.h), but the compiler keeps complaining about "GetDpiForWindow" or "AdjustWindowRectExForDpi" not being defined.
Code example:
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;
int main()
{
int dpi = GetDpiForWindow(GetConsoleWindow());
return 0;
}
Error:
error: 'GetDpiForWindow' was not declared in this scope; did you mean
'GetTopWindow'?

Log4cplus not compiling as descirbed

#include <log4cplus/configurator.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/initializer.h>
#include <log4cplus/layout.h>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <string>
int main(int argc, char* argv[]) {
log4cplus::SharedAppenderPtr appender(new log4cplus::FileAppender("test.log"));
appender->setName("mainLog");
std::string pattern = "[%-5p][%D{%Y/%m/%d %H:%M:%S:%q}][%-l][%t] %m%n";
std::auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(pattern));
appender->setLayout(layout);
mainLog = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("mainLog"));
mainLog.addAppender(appender);
LOG4CPLUS_INFO(mainLog, LOG4CPLUS_TEXT("Execution started!"));
return 0;
}
The above code should work according to the every guide I have managed to find on the subject, but it won't compile. The line:
appender->setLayout(layout);
underscores layout and says:
cannot convert argument 1 from 'std::auto_ptr<log4cplus::Layout>' to 'std::unique_ptr<log4cplus::Layout,std::default_delete<_Ty>>
What gives?

no member named 'set' in 'std::vector<int>'

I use QT creator and Xcode 4.63 (osx mav)
I just call .set(....) and got following error
no member named 'set' in 'std::vector<int, std::allocator<int> >
seem like something wrong with lib , since I'm new to QT creator i still got no idea how to make it right. (I'm new to c++)
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "console.h"
#include "filelib.h"
#include "grid.h"
#include "gwindow.h"
#include "simpio.h"
#include "vector.h"
using namespace std;
int main() {
vector<int> myVector(10,0);
fstream mystream;
string getStream;
while(getline(mystream,getStream)) {
getline(mystream,getStream);
if(stringToInteger(getStream)>=0 && stringToInteger(getStream)<=9) {
myVector.set(0,(myVector[0]+1))
};
}
return 0;
};
The "set" method doesn't exist for vectors in STL. You could use the [] operator if you want to change a value:
myVector[0] = myVector[0] + 1;
or even
myVector[0]++;

error: 'int main(int, char**)' previously defined here in C++

I'm implementing gtest now, and it gives me an error : main previously defined here.
Here's utest.cpp
// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>
// Declare a test
TEST(TestSuite, testCase1)
{
EXPECT_EQ(5,getX(5));
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And here's the code that i'm testing
test.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>
#include <sstream>
#include "math.h"
int getX(int x)
{
return x;
}
int main(int argc, char **argv)
{
return 0;
}
There's nothing in test.cpp main but actual code will have some codes in main.
I dont have header files for utest and test cpp files
I tried
#ifndef UTEST_H
#define UTEST_H
and didn't solve the error.
The error message states what the problem is, you have two main() functions. I believe you want to remove the duplicate main() from test.cpp.