Failling Qt application on Ubuntu - c++

I have Ubuntu 10.04 and have the Qt library install. When I run the code
#include <QDir>
#include <QFileInfo>
#include <QtDebug>
int main( int argc, char **argv )
{
foreach( QFileInfo drive, QDir::drives() )
{
qDebug() << "Drive: " << drive.absolutePath();
QDir dir = drive.dir();
dir.setFilter( QDir::Dirs );
foreach( QFileInfo rootDirs, dir.entryInfoList() )
qDebug() << " " << rootDirs.fileName();
}
return 0;
}
I get the following errors.
g++ qt.cpp -o test
qt.cpp:1:16: error: QDir: No such file or directory
qt.cpp:2:21: error: QFileInfo: No such file or directory
qt.cpp:4:19: error: QtDebug: No such file or directory
qt.cpp: In function ‘int main(int, char**)’:
qt.cpp:8: error: ‘QFileInfo’ was not declared in this scope
qt.cpp:8: error: ‘QDir’ has not been declared
qt.cpp:8: error: ‘foreach’ was not declared in this scope
qt.cpp:9: error: expected ‘;’ before ‘{’ token
How do I fix this problem?

g++ seems to not find Qt includes files.
You should add an include directory when compiling. and linked with the Qt library.

Related

How to compile a c++ code using libtorrent?

I am testing the example code posted on the official libtorrent website (https://www.libtorrent.org/tutorial-ref.html). I pasted the code here:
#include <libtorrent/session.hpp>
#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/torrent_handle.hpp>
#include <libtorrent/magnet_uri.hpp>
int main(int argc, char const* argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s <magnet-url>\n");
return 1;
}
lt::session ses;
lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1]);
atp.save_path = "."; // save in current dir
lt::torrent_handle h = ses.add_torrent(atp);
// ...
}
I have already installed the libtorrent:
ldconfig -v | grep libtorrent
libtorrent-rasterbar.so.9 -\> libtorrent-rasterbar.so.9.0.0
I used the following command to compile the code:
g++ main.cpp -o run -ltorrent-rasterbar -lboost_filesystem-mt
However, I got errors:
main.cpp: In function 'int main(int, const char\*\*)':
main.cpp:12:3: error: 'lt' has not been declared
lt::session ses;
I also saw another solution, but it does not resolve the issue I am facing: How to compile a libtorrent(rasterbar) code ?
Does anyone know what caused this failure?

loading rosbag duration using cpp

I am interested to load contents of rosbag into databse using sqlite and c++.
while including rosbag/view.h and rosbag/bag.h header file in my cpp file in visual studio code I am facing error of no such file or directory
code: ref http://wiki.ros.org/rosbag/Cookbook#C.2B-.2B-
#include <ros/ros.h>
#include <rosbag/bag.h>
#include <rosbag/view.h>
int main(int argc, char **argv)
{
rosbag::Bag bag;
bag.open("input.bag", rosbag::bagmode::Read);
rosbag::View view(bag);
ros::Time bag_begin_time = view.getBeginTime();
ros::Time bag_end_time = view.getEndTime();
std::cout << "ROS bag time: " << (bag_end_time-
bag_begin_time).toSec() << "(s)" << std::endl;
bag.close();
return 0;
}
error:
main.cpp:2:10: fatal error: rosbag/bag.h: No such file or directory
2 | #include <rosbag/bag.h>
| ^~~~~~~~~~~~~~
Issue is resolved by including ros package, directories and libraries in the CmakeLists.txt and executing the code with cmake and make respectively now able to see the rosbag duration.
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(esw2 VERSION 0.0.1 LANGUAGES CXX)
find_package(rosbag REQUIRED)
add_executable(folder_name
file.cpp
)
target_include_directories(bag_reader
PUBLIC
include
${rosbag_INCLUDE_DIRS}
)
target_link_libraries(bag_reader
PRIVATE
${rosbag_LIBRARIES}
stdc++fs
)

main.cpp:1:10: fatal error: opencv2/highgui.hpp: No such file or directory

I have install opencv in ubuntu 18.04 and it was installed successfully, I have tried this command:
$ pkg-config --modversion opencv
and its output is: 4.0.1-dev
after this i have tried to rum c++ code:
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std;
int main( int argc, char** argv ) {
cv::Mat image;
image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR);
if(! image.data ) {
std::cout << "Could not open or find the image" << std::endl ;
return -1;
}
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
cv::imshow( "Display window", image );
cv::waitKey(0);
return 0;
}
with this command: :~/cpp_test$ g++ main.cpp -o output pkg-config --cflags --libs opencv
but it throws a fatal error:
main.cpp:1:10: fatal error: opencv2/highgui.hpp: No such file or directory
#include <opencv2/highgui.hpp>
^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
I have reviewed some similar questions but i did not find my answer, i think this is because of environment variables and i do not know which variables i have to set.
In the compiling command add a "4" next to "opencv" (or the number of your version of OpenCV):
$ g++ main.cpp -o output \`pkg-config --cflags --libs opencv4\`

SDL2 Install problems: makefile.win file not recognized: file format not recognized and collect2.exe [error] id return1 exit status

After I learned the very basics of cpp i decided to push myself ahead and try SDL2 and try to make a game. I found the lazy foo's SDL tutorials. I tried to follow it but i seemed to have problems with installation. After putting in a "test" code i tried compiling it, and these messages showed up on the log:
C:\File\Location\For\My\Project\Makefile.win file not recognized: File format not recognized
C:\File\Location\For\My\Project\collect2.exe [Error] ld returned 1 exit status
I think it might be a linking error and heres my linkers:-lmingw32-lSDL2main-lSDL2
I tried deleting this Makefile.win but the same message just showed up and there isn't even a collect2.exe
I'm using the Orwell Dev-C++ using the mingw gcc 4.8.1 32bit release compiler, and heres the code:
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char **argv){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}

Trying to compile clang API project using g++

I have just downloaded llvm and clang with svn, built it just like on the official site described and want to use the clang API. I have an example, which won't to be compiled=(
#include <iostream>
#include <clang-c/Index.h>
int main (int argc, char** argv)
{
CXIndex index = clang_createIndex (
false, // excludeDeclarationFromPCH
true // displayDiagnostics
);
CXTranslationUnit unit = clang_parseTranslationUnit (
index, // CIdx
"main.cpp", // source_filename
argv + 1 , // command_line_args
argc - 1 , // num_command_line_args
0, // unsave_files
0, // num_unsaved_files
CXTranslationUnit_None // options
);
if (unit != 0 )
std::cout << "Translation unit successfully created" << std::endl;
else
std::cout << "Translation unit was not created" << std::endl;
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
}
Command line looks like this:
g++ main.cpp -I/home/<user>/llvm/tools/clang/include
-L/home/<user>/build/Debug+Asserts/lib/ -llibclang -o main
UPDATE:
The error:
/usr/bin/ld: cannot find -llibclang
But libclang is in the /home//build/Debug+Asserts/lib/ directory.
Also tried using -llibclang.so except the llibclang - no matter=( ld says that it can not find the lib.
Anything helps, thank you!