Including external libraries in CodeRunner 2 app? - c++

I've always used Xcode to compile OpenCV based code in c++. The procedure in Xcode was quite simple, I just had to mention the paths and add the necessary lib files to the project. Theres this app called CodeRunner 2 for macOS. Theres no proper documentation on how to include external libraries to compile code in this app. Is it possible to link OpenCV headers and compile them in CodeRunner ? If yes, could someone post the steps?

You can run OpenCV in CodeRunner by setting up a new language. Go to Preferences -> Languages, right-click C++, and select Duplicate. Name the new language "C++ OpenCV". On the right side of the preferences window, click Settings then the Edit Script button. Look for this line (or something similar):
xcrun clang++ -x c++ -lc++ -o "$out" "${files[#]}" "${#:1}"
Add the clang++ command line parameters for OpenCV after "$out". Here's my version:
xcrun clang++ -x c++ -lc++ -o "$out" -I/usr/local/opt/opencv3/include -L/usr/local/opt/opencv3/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lopencv_calib3d "${files[#]}" "${#:1}"
Modify the -I and -L parameters to match your OpenCV install path. On this machine I used Homebrew to install OpenCV so it was installed in /usr/local/opt. On other machines I've compiled from source so OpenCV is installed in /usr/local/lib.
Modify the -l parameters to include the libraries you typically use.
After saving the compile script, go back to Preferences -> Languages and select the Templates button. You can set up a template for OpenCV programs. Here's mine:
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
int main(int argc, char *argv[]) {
cv::Mat image;
// read an image
if (argc < 2)
image = cv::imread("img.jpg");
else
image = cv::imread(argv[1]);
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
// create image window named "asdfasdf"
cv::namedWindow("asdfasdf");
// show the image on window
cv::imshow("asdfasdf", image);
// wait for key
cv::waitKey(0);
return 0;
}

The previous reply by SSteve is great and also helps me sort out linking Boost library in CodeRunner.
Because the solution in the previous reply is specific to OpenCV library, a carelessly adding to the clang++ command line for external libraries in general might just generate massive building errors, which was the case when I tried to link Boost library.
Here, I want to clarify the unclear bit in SSteve's reply so everyone knows how and where to modify the command line before compiling their code with external library in Mac OS system.
I will use my case to explain, but in some point I will inform you of the tricky bits in CodeRunner setting or general command line typing.
I use macport to install the Boost library by
sudo port install boost
header file is located at /opt/local/include
library is located at /opt/local/lib/
If you cannot find the specific sub-library in Boost, open your terminal and type
cd /opt/local/lib/
find . -iname "*boost*"
and you should see all sub-libraries of Boost ( static library ends with .a and dynamic library ends with .dylib ) as below.
Before you start to modify the original command line ( supporting c++ 14 version ) such as
xcrun clang++ -x c++ -std=c++14 -stdlib=libc++ -lc++ -o "$out" "${files[#]}" "${#:1}" ${CR_DEBUGGING:+-g}
you need to know the directory of header file is after -I and the directory of Boost library is after -L, like
-I /opt/local/include/
-L /opt/local/lib/
In order to use a compiled static or dynamic sub-library in Boost ( see figure above ), you have to include it specifically after -L /opt/local/lib/. However, simply copying the library name without file extension either .a or .dylib would never let CodeRunner find the library you expect to run !!!
The detail is explained here and I just quota the important bit below
clang -dynamiclib -o libtest.dylib file1.o file2.o -L/some/library/path -lname_of_library_without_lib_prefix
To run such an example code in Boost Quickstart Document
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
the way to include <boost/regex.hpp> now is by
xcrun clang++ -x c++ -std=c++14 -stdlib=libc++ -lc++ -o "$out" -I /opt/local/include/ -L /opt/local/lib -lboost_regex-mt "${files[#]}" "${#:1}" ${CR_DEBUGGING:+-g}
By using this command line, you should be able to compile the example code with Boost library.
Just remember to replace the prefix -lib with -l and exclude the file extension in the command line.
At last, there are some alternative solution to include the external library by using Xcode, which is in here

Related

Difficulty linking Boost 1.60 on OS X after installing via Homebrew

I have a fresh install of OS X 10.11.4 that I immediately installed the Xcode toolchain on, then Homebrew, then Boost 1.60. In order to test that everything had gone well, I wrote the following code on my Desktop.
#include <iostream>
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path new_directory("hello");
boost::filesystem::create_directory(new_directory);
}
I then attempted to compile it as I usually have done with the following command.
$ clang++ test.cpp -o test -lboost_system -lboost_filesystem
I received the following error.
test.cpp:3:10: fatal error: 'boost/filesystem.hpp' file not found
#include <boost/filesystem.hpp>
This is how I have always compiled projects that link Boost in the past. I'm assuming that I have probably forgotten a step along the way that allows clang to search a specific path to dynamically link the libraries. What should I change in order for this compilation command to work?
For me, boost has been compiled and installed into a subdirectory of my home directory, so you'll need to modify the paths as appropriate for your homebrew installation:
flags="-std=c++1z -I/${HOME}/local/include -L${HOME}/local/lib -lboost_filesystem -lboost_system"
c++ ${flags} -o jared jared.cpp
First get the location of boost by doing the following:
brew info boost
From the image above, you can see that my location is
/usr/local/Cellar/boost/1.66.0
Then, to compile, use the following:
c++ -I /usr/local/Cellar/boost/1.66.0 main.cpp -o boost

openCV program compile error "libopencv_core.so.2.4: cannot open shared object file: No such file or directory" in ubuntu 12.04

I compiled and installed openCV 2.4.2 in ubuntu 12.04. Under /usr/local/include I can see the directories /usr/local/opencv and /usr/local/opencv2.
Here is the code I wrote:
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc,char **argv)
{
Mat image;
image = imread(argv[1],1);
if(argc != 2 || !image.data)
{
cout << "No image data\n";
return -1;
}
namedWindow("Display Image",CV_WINDOW_AUTOSIZE);
imshow("Display Image",image);
waitKey(0);
return 0;
}
I compiled it using this command line:
g++ DisplayImage.cpp -o DisplayImage `pkg-config opencv --cflags --libs`
There were no compile time errors, however when I try to run the resulting binary with /DisplayImage code.png I get the following error message:
./DisplayImage: error while loading shared libraries: libopencv_core.so.2.4: cannot open shared object file: No such file or directory
You haven't put the shared library in a location where the loader can find it. look inside the /usr/local/opencv and /usr/local/opencv2 folders and see if either of them contains any shared libraries (files beginning in lib and usually ending in .so). when you find them, create a file called /etc/ld.so.conf.d/opencv.conf and write to it the paths to the folders where the libraries are stored, one per line.
for example, if the libraries were stored under /usr/local/opencv/libopencv_core.so.2.4 then I would write this to my opencv.conf file:
/usr/local/opencv/
Then run
sudo ldconfig -v
If you can't find the libraries, try running
sudo updatedb && locate libopencv_core.so.2.4
in a shell. You don't need to run updatedb if you've rebooted since compiling OpenCV.
References:
About shared libraries on Linux: http://www.eyrie.org/~eagle/notes/rpath.html
About adding the OpenCV shared libraries: http://opencv.willowgarage.com/wiki/InstallGuide_Linux
To make it more clear(and to put it together) I had to do Two things mentioned above.
1- Create a file /etc/ld.so.conf.d/opencv.conf and write to it the paths of folder where your opencv libraries are stored.(Answer by Cookyt)
2- Include the path of your opencv's .so files in LD_LIBRARY_PATH ()
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv/lib
Find the folder containing the shared library libopencv_core.so.2.4 using the following command line.
sudo find / -name "libopencv_core.so.2.4*"
Then I got the result:
/usr/local/lib/libopencv_core.so.2.4.
Create a file called
/etc/ld.so.conf.d/opencv.conf
and write to it the path to the folder where the binary is stored.For example, I wrote /usr/local/lib/ to my opencv.conf file.
Run the command line as follows.
sudo ldconfig -v
Try to run the command again.
Umair R's answer is mostly the right move to solve the problem, as this error used to be caused by the missing links between opencv libs and the programme. so there is the need to specify the ld_libraty_path configuration.
ps. the usual library path is suppose to be:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
I have tried this and it worked well.
Add this link:
/usr/local/lib/*.so.*
The total is:
g++ -o main.out main.cpp -I /usr/local/include -I /usr/local/include/opencv -I /usr/local/include/opencv2 -L /usr/local/lib /usr/local/lib/*.so /usr/local/lib/*.so.*

Using the c++ Boost regex libraries in Arch Linux

I'm having trouble building a simple c++ program that tests out regex's from the boost library. The problem that I'm having is occurring in the linking stage and I don't know enough to fix the error on my own.
In the .cpp program that uses regexes I used the following include line.
#include <boost/regex.hpp>
I don't know enough to figure out what command I should use to build the program using g++. I tried using the following command line (and variations of it) to build the program.
g++ -I/usr/include/regex -L/usr/lib -llibboost_regex main.cpp -o regex_test
Other information that might be relevant:
Operating system: Arch linux
g++ version: 4.6.2 20120120 (prerelease)
Any help would be appreciated.
Assume you have installed Boost with the boost and boost-libs packages,
The header <boost/regex.hpp> should exist in /usr/include/boost/regex.hpp. You don't need to use any -I flags since /usr/include should be included by default.
You shouldn't need the -L flag either since /usr/lib should also be included by default when linking.
When using the -l flag to link with a library libfoo.so, the leading "lib" part should be removed.
The command line should therefore be:
g++ main.cpp -o regex_test -lboost_regex

Problems compiling gtkmm

OS: Fedora 14
Compiler: g++ (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4)
I installed gtkmm24-devel from repository via yum. To make sure the install went as planned I decided to try one of the examples on the page.
#include <gtkmm.h>
int main(int argc, char *argv[]) {
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Main::run(window);
return 0;
}
I ran the example, and, hey! It said it couldn't find gtkmm.h, no problem, I just forgot to link the library. I added /usr/include/gtkmm-2.4 to my library search through Eclipse. No bueno, g++ still can't find it!
fatal error: gtkmm.h: No such file or directory
I then try to include gtkmm by using #include <gtkmm-2.4/gtkmm.h> and recompile, another error! :(
/usr/include/gtkmm-2.4/gtkmm.h:87:20: fatal error: glibmm.h: No such file or directory
Thanks for reading.
Short answer
Use the output of 'pkg-config gtkmm-2.4 --cflags' for include paths and 'pkg-config gtkmm-2.4 --libs' for libraries to link.
Long answer
It said it couldn't find gtkmm.h, no problem, I just forgot to link the library.
Building a C/C++ program is done in two separate steps. First the source files are compiled, outputting object files; and then the object files are linked together. The error you are getting comes from the compiling step.
On Linux, most libraries come with pkgconfig files to make it easier for other programs to use the libraries. gtkmm also comes with its own pkgconfig files.
You are trying to manually specify /usr/include/gtkmm-2.4 for include path; this is wrong. Instead, use the output of pkgconfig to figure out where the header files are located. To get all the include directories needed for gtkmm, use the following command:
pkg-config gtkmm-2.4 --cflags
For linking, use the following pkgconfig command to get the libraries you need to link with:
pkg-config gtkmm-2.4 --libs
You can test it on the command line by invoking g++ directly.
g++ myfirstprogram.cpp -o myfirstprogram `pkg-config gtkmm-2.4 --cflags --libs`
For more information, see the gtkmm docs: http://library.gnome.org/devel/gtkmm-tutorial/unstable/sec-basics-simple-example.html.en
These steps usually help resolving this problem:
Search your computer for glibmm.h
If found - add its directory to the include path list
If not found - Google for glibmm.h and find out which library it is contained in. You will find out in this case it's (surprise!) glibmm. Install it using your package manager.
The problem, as noted in comments, is a compiler error and the compiler is arguing about a missing (header) file. The steps I described above either find the location of the missing file or help you to install a library that the header file belongs to.

xerces-c 2.8 : error while loading shared libraries

I'm trying to compile a program running on an HP UX server on a Red Hat Linux.
It uses xerces-c library to parse xml files. Compilation is ok, but when i try to run it, I get the following message
./a.out: error while loading shared
libraries: libxerces-c.so.28: cannot
open shared object file: No such file
or directory
I wrote a very simple program to try and understand whats going on:
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/TransService.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
int main(int argc, char* argv[])
{
return 0;
}
And compiled it like this:
g++ test.cpp
-L./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
Surprisingly the file is actually there:
lib]$ ls
libxerces-c.a libxerces-c.so.28 libxerces-depdom.a libxerces-depdom.so.28
libxerces-c.so libxerces-c.so.28.0 libxerces-depdom.so libxerces-depdom.so.28.0
Any thoughts ? I feel i'm missing something, but don't know what.
Thanks in advance.
run ldd a.out and see if the linker can resolve the right .so file
export LD_LIBRARY_PATH to include the current folder (in the same manner as the PATH variable) and check ldd again
the good way to do what you want is the following one:
g++ test.cpp -Xlinker -R ./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
or
g++ test.cpp -Wl,-rpath ./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
Xlinker or Wl options allow you to use specific linking options, you do not need to modifiy
LD_LIBRARY_PATH
You need to tell the runtime c library where to find the various symbols that arent compiled statically in your code and arent in the usualy /lib and /usr/lib locations.
You do this by adding the path to your shared library to LD_LIBRARY_PATH. In this case, this will be what you have been putting for the -L argument to the compiler.