Linking to OpenCV under MacOSX - c++

I'm trying to compile a C++/OpenCV Code in MacOS X Snow Leopard which was originally under Linux. I installed OpenCV 2.2 using MacPorts, but I cannot use something like this anymore:
g++ -o Localization Localization.o DataReader.o Kalman.o -libopencv_core -libopencv_highgui -L/usr/local/lib/
I'm getting this error:
ld: library not found for -libopencv_core
collect2: ld returned 1 exit status
make: *** [Localization] Error 1
I tried other variations; like -lcv, -lhighgui which I originally used to use, but I'm not sure what to use here.

If you're using macports, you're using the wrong paths. It should be -L/opt/local/lib, not /usr/local. Also, to make things easier, you should just use pkg-config:
g++ -o Localization Localization.o DataReader.o Kalman.o $(pkg-config --libs opencv)
(that assumes that you have /opt/local/bin in your PATH). This way you can use the same line for linux and OS X.

The link flag should be -lopencv_core not -libopencv_core. The latter will look for a file called libibopencv_core.{dylib,a} instead of libopencv_core.{dylib,a}. The same for opencv_highgui.

Related

How do I install and use PNGwriter to write PNG image files?

I'd like to learn how to write PNG images pixel-by-pixel using both RGB and HSV color models with C++. I read that this should be fairly easy using PNGwriter (https://github.com/pngwriter/pngwriter), but I've spent many hours struggling with installing it (on Ubuntu) and compiling my code with it. Any help would be much appreciated.
Disclaimer: I have a weird background in the sense that I have many years of experience in using Unix-like operating systems, doing stuff in the terminal, and writing code, but I know little/nothing about installing software from the source code or compiling programs manually or with makefiles from multiple source code files.
The installation instructions on GitHub advise to do one of the following:
Spack:
spack install pngwriter
spack load pngwriter
From Source:
First install the dependencies zlib, libpng, and (optional for text support) freetype. PNGwriter >can then be installed using CMake:
git clone https://github.com/pngwriter/pngwriter.git
mkdir -p pngwriter-build
cd pngwriter-build
# for own install prefix append: -DCMAKE_INSTALL_PREFIX=$HOME/somepath
cmake ../pngwriter
make -j
# optional
make test
# sudo is only required for system paths
sudo make install
I managed to install Spack and then PNGwriter, but couldn't compile the simplest program with it and wasn't able to figure out why. I then installed PNGwriter manually, but still couldn't compile anything with it. This was many hours of struggling ago so I, unfortunately, don't remember what kind of errors or problems I was encountering at this point.
The instructions on GitHub say the following about linking:
First set the following environment hint if PNGwriter was not installed in a system path:
# optional: only needed if installed outside of system paths
export CMAKE_PREFIX_PATH=$HOME/somepath:$CMAKE_PREFIX_PATH
Use the following lines in your projects CMakeLists.txt:
find_package(PNGwriter 0.7.0)
if(PNGwriter_FOUND)
target_link_libraries(YourTarget PRIVATE PNGwriter::PNGwriter)
endif(PNGwriter_FOUND)
Questions: How do I know if PNGwriter was or wasn't installed in a system path? I have libPNGwriter.a in /usr/local/lib and pngwriter.h in /usr/local/include --- does this mean that it was installed in a system path? When installing I simply tried to follow the instructions above. Do I just type the environment hint to the terminal or add it to some file? If the former then does it need to be given every time I open a new terminal session? Is "somepath" /usr/local/lib, /usr/local/include or something else?
Questions: Does the second part regarding "CMakeLists.txt" depend on whether PNGwriter was installed in a system path? What is "CMakeLists.txt"? I assume it's some file one's IDE creates, but my NetBeans projects don't seem to contain such files. What if I have a single source file and compile it manually in the terminal?
Now, let's say I'd like to compile the PNGwriter quickstart example:
#include <pngwriter.h>
int main()
{
int i;
int y;
pngwriter png(300, 300, 0, "test.png");
for(i = 1; i ≤ 300; i++)
{
y = 150 + 100*sin((double)i*9/300.0);
png.plot(i, y, 0.0, 0.0, 1.0);
}
png.close();
return 0;
}
The PNGwriter manual instructs to compile as
g++ myprogram.cc -o my_program `freetype-config --cflags` -I/usr/local/include -L/usr/local/lib -lpng -lpngwriter -lz -lfreetype
but I get errors
$ g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include -L/usr/local/lib -lpng -lpngwriter -lz -lfreetype
Package freetype2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
Package 'freetype2', required by 'virtual:world', not found
Package freetype2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
Package 'freetype2', required by 'virtual:world', not found
Package freetype2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
Package 'freetype2', required by 'virtual:world', not found
Package freetype2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
Package 'freetype2', required by 'virtual:world', not found
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
In file included from example.cpp:1:0:
/usr/local/include/pngwriter.h:66:22: fatal error: ft2build.h: No such file or directory
compilation terminated.
The answer to this Stack Overflow question (Trying to install pygame on ubuntu which gives error) suggests to install libfreetype6-dev, but I apparently have the latest version already whereby the errors remain unchanged. If I instead actually add the directory containing freetype2.pc (I found it by going to / and using find -name "freetype2.pc") to the environment variable (added export PKG_CONFIG_PATH="/usr/lib/x86_64-linux-gnu/pkgconfig/:$PKG_CONFIG_PATH to ~/.bashrc and did source ~/.bashrc) then I get new errors
$ g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include -L/usr/local/lib -lpng -lpngwriter -lz -lfreetype
/usr/bin/ld: cannot find -lpngwriter
collect2: error: ld returned 1 exit status
Here I figured out that I needed to replace -lpngwriter with -lPNGwriter (i.e., the manual is erroneous). Then I get:
$ g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include -L/usr/local/lib -lpng -lPNGwriter -lz -lfreetype
/usr/bin/ld: /usr/local/lib/libPNGwriter.a(pngwriter.cc.o): undefined reference to symbol 'png_set_sig_bytes##PNG12_0'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpng.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
This is where I'm currently stuck. I can't seem to find a solution by googling (at least one that I'd understand).
Question: How do I get this working? How do I get it working in NetBeans? Do I get these problems because I effed up the linking step above?
Edit1: As per john's comment, I tried swapping -lpng and -lPNGwriter and I again get new errors:
$ g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include -L/usr/local/lib -lPNGwriter -lpng -lz -lfreetype
/usr/local/lib/libPNGwriter.a(pngwriter.cc.o): In function `pngwriter::close()':
pngwriter.cc:(.text+0x41c2): undefined reference to `png_convert_to_rfc1123_buffer'
/usr/local/lib/libPNGwriter.a(pngwriter.cc.o): In function `pngwriter::read_png_info(_IO_FILE*, png_struct_def**, png_info_def**)':
pngwriter.cc:(.text+0x4fdf): undefined reference to `png_set_longjmp_fn'
collect2: error: ld returned 1 exit status
I'm still left clueless.
Thanks to john, I think I got it figured out. My guess is that the installation from the source (after the Spack installation) messed things up somehow. I reinstalled PNGwriter using Spack and, now apparently having all the pieces for the compilation command, was finally able to compile the example code.
Summary:
Source Spack
# For bash/zsh/sh
$ . spack/share/spack/setup-env.sh
# For tcsh/csh
$ source spack/share/spack/setup-env.csh
# For fish
$ . spack/share/spack/setup-env.fish
Install using Spack (skip if already installed)
spack install pngwriter
Load PNGwriter (I guess one needs to do this in every new terminal session)
spack load pngwriter
Compile (note that this isn't quite what the PNGwriter manual suggests)
g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include -L/usr/local/lib -lPNGwriter -lpng -lz -lfreetype

Building GDAL with all libraries static

I want to develop a small program that checks which polygons from a shapefile intersect a given rectangle. This program is to be used in a website (with PHP's exec() command). The problem is, my webserver cannot install GDAL, for reasons unknown to me. So I can't link to the shared libraries. Instead, I must link to static libraries, but these aren't given.
I've downloaded the GDAL source code from here (2.3.2 Latest Stable Release - September 2018), and followed the build instructions from here. Since I already have GDAL working on my Debian, and don't want to mess with it, I followed the "Install in non-root directory" instructions, with some adjusts from the last item in the "Some caveats" section:
cd /home/rodrigo/Downloads/gdal232/gdal-2.3.2
mkdir build
./configure --prefix=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/ --without-ld-shared --disable-shared --enable-static
make
make install
export PATH=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/bin:$PATH
export LD_LIBRARY_PATH=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib:$LD_LIBRARY_PATH
export GDAL_DATA=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/share/gdal
/usr/bin/gdalinfo --version
build/bin/gdalinfo --version
The first /usr/bin/gdalinfo --version gives 2.1.2 (the previous installed version). The second, build/bin/gdalinfo --version, gives 2.3.2 (the version just built).
By now, my program only uses the ogrsf_frmts.h header, which is in /usr/include/gdal/ or /home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/include/ directory, depending on the build. There's no ogrsf_frmts.a file, but only a libgdal.a. Is this the file I should be linking against? If so, how? I've tried so far:
gcc geofragc.cpp -l:libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l:libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l:/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp /home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -l/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -l:/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
but nothing works. What am I missing?
EDIT
The second trial (gcc geofragc.cpp -Wl,-Bstatic -l:libgdal.a) is giving the following error:
/usr/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-linux-gnu/6/../../../../lib/libgdal.a(gdalclientserver.o): In function `GDALServerSpawnAsync()':
(.text+0x1f5e): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
You can use the gdal-config program to get correct options for compilation and linking. This program is a part of the GDAL library and it has its own options:
hekto#ubuntu:~$ gdal-config --help
Usage: gdal-config [OPTIONS]
Options:
[--prefix[=DIR]]
[--libs]
[--dep-libs]
[--cflags]
[--datadir]
[--version]
[--ogr-enabled]
[--gnm-enabled]
[--formats]
You have to make sure this program is on your search path, or you can create an alias - for example:
alias gdal-config='/home/rodrigo/Downloads/gdal232/gdal-2.3.2/bin/gdal-config'
Now your compilation and linking command becomes the following one:
g++ `gdal-config --cflags` geofragc.cpp `gdal-config --libs` `gdal-config --dep-libs`
You have to use the g++ compiler to link with C++-built libraries.
Another option is to create a Makefile with these lines:
CXXFLAGS += ${shell gdal-config --cflags}
LDLIBS += ${shell gdal-config --libs}
LDLIBS += ${shell gdal-config --dep-libs}
geofragc: geofragc.cpp
and just call make with this Makefile.
I hope, it'll help.

LLVM tutorial --system-libs does not exist

I have been working through the Kaleidoscope tutorial for LLVM and have been having endless problems compiling it. In the following tutorial it appears that the flag "--system-libs" doesn't exist for llvm-config (it simply prints out a usage block). I have tried leaving it out but it seems like a rabbit hole of linker errors which leads me to believe I have just set up my development environment completely wrong. I have tried it both on OSX Yosemite and Ubuntu with similar results. The not found error can be resolved by adding -I [path to llvm] however this just exposes more errors making me think that is the wrong approach.
http://llvm.org/releases/3.6.0/docs/tutorial/LangImpl3.html
$make
#clang++ -g -v -L -std=c++11 -O3 toy.cpp -I/usr/include/llvm-3.5/ `llvm-config --cxxflags --ldflags --libs all` -o toy
clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy
usage: llvm-config <OPTION>... [<COMPONENT>...]
Get various configuration information needed to compile programs which use
LLVM. Typically called from 'configure' scripts. Examples:
llvm-config --cxxflags
llvm-config --ldflags
llvm-config --libs engine bcreader scalaropts
Options:
--version Print LLVM version.
--prefix Print the installation prefix.
--src-root Print the source root LLVM was built from.
--obj-root Print the object root used to build LLVM.
--bindir Directory containing LLVM executables.
--includedir Directory containing LLVM headers.
--libdir Directory containing LLVM libraries.
--cppflags C preprocessor flags for files that include LLVM headers.
--cflags C compiler flags for files that include LLVM headers.
--cxxflags C++ compiler flags for files that include LLVM headers.
--ldflags Print Linker flags.
--libs Libraries needed to link against LLVM components.
--libnames Bare library names for in-tree builds.
--libfiles Fully qualified library filenames for makefile depends.
--components List of all possible components.
--targets-built List of all targets currently built.
--host-target Target triple used to configure LLVM.
--build-mode Print build mode of LLVM tree (e.g. Debug or Release).
Typical components:
all All LLVM libraries (default).
engine Either a native JIT or a bitcode interpreter.
toy.cpp:1:10: fatal error: 'llvm/IR/Verifier.h' file not found
#include "llvm/IR/Verifier.h"
^
1 error generated.
make: *** [parser] Error 1
Turns out the version of llvm-config I am using is out of date. On Ubuntu I can just apt-get install llvm-config-3.6 and on OSX I can do a brew install homebrew/versions/llvm36. Finally use llvm-config-3.6 instead of just llvm-config.

How to change to use the g++ mingw toolchain under cygwin

It looks like g++/gcc-mingw is installed but I can't tell how to use it.
I am building a C source file with a windows main proc and I get this error:
g++-3 -mwindows -L/usr/lib/gcc/i686-pc-mingw32 -lmingw32 winmain.c
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld:
cannot find -lmingw32 collect2: ld returned 1 exit status
Or
$ g++-3 -mwindows -L/usr/lib/gcc/i686-pc-mingw32 winmain.c
/tmp/ccyLGxkn.o:winmain.c:(.text+0x21): undefined reference to
`MainWndProc(HWND__*, unsigned int, unsigned int, long)#16' collect2:
ld returned 1 exit status
If I do a list directory on the installed libraries I see this:
$ ls /usr/lib/gcc/ i686-pc-cygwin/ i686-pc-mingw32/
Note: 'i686-pc-mingw32' How do I compile using the mingw32 libraries?
Also, I installed the package: 'gcc-mingw-g++' through the cygwin setup.exe utility, I just don't know how to use it?
Note: I am mostly working with 'cygwin', I would prefer NOT to install the full ming install?
There are actually two sets of projects, the libraries and the actually toolchain. I installed the libraries and not the toolchain.
The libraries are: gcc-mingw-g++
mingw64-i686-gcc is the toolchain
I ran with this command and it worked correctly:
i686-pc-mingw32-g++
i686-w64-mingw32-g++
Or:
i686-w64-mingw32-g++ -mwindows -static -I/opt/jdk/include
-I/opt/jdk/include/win32 -Wl,--add-stdcall-alias -shared -o Hello.dll Hello.cpp

ld can't link with a main executable

On OSX 10.6.4 with i686-apple-darwin10-g++-4.2.1 compiling using TextMate and a Makefile which in the first place has been made für a Linux and I am trying to translate for OSX.
When compiling a c++ project I get the "can't link with a main executable" error:
g++ -Wall -g -I ~/svnX-Repository/axp-Projekte/xrlfupa/trunk/src/ -I ~/svnX-Repository/boost_1_44_0 -I /opt/local/var/macports/software/boost/1.44.0_0/opt/local/lib/ -I /opt/local/var/macports/software/gsl/1.14_0/opt/local/include/ -o xrfLibTest xrfLibTest.o excitFunctions.o xrfFunctions.o filterFunctions.o detectorFunctions.o -L/opt/local/var/macports/software/boost/1.44.0_0/opt/local/lib/ -L/opt/local/var/macports/software/gsl/1.14_0/opt/local/lib/ -lm -lxrlTUB -lboost_serialization -lgsl -lgslcblas # Debug 1
ld: in /usr/local/lib/libxrlTUB.so, can't link with a main executable
collect2: ld returned 1 exit status
make: *** [prog] Error 1
The library that is mentioned (libxrlTUB.so) is in its place (/usr/local/lib/libxrlTUB.so) but, possibly that is where the problem came from, the libxrlTUB.so has been compiled by myself beforehand as well.
The compile process went through, it was generated by swig, though there was a warning:
g++ -arch x86_64 -m32 -g -fpic -I /usr/include/python2.6 -c PyXrl_wrap.cxx
In function 'void SWIG_Python_AddErrorMsg(const char*)':
warning: format not a string literal and no format arguments
which, as far as I could find out, shouldnt be a problem. (Or is it?)
Unfortunately this whole thing is part of a project from the university. Actually I am supposed to write an X-ray-analysis script in python, which would be fine, if... well if I wouldn't be expected to use the librarys that are meant to result from this c++ project.
(Afterwards they should be used via import in python.)
I am not really experienced with c++, neither with compiling on OSX systems. So far I have been bothering with scipting (python, bash, etc). So Maybe I am just missing something simple. Hopefully someone can give me an hint where I can continue reading in order to deal with the above "can't link with a main executable" error...
Thanx in advance,
Liam
The error message is telling you the problem—it is that /usr/local/lib/libxrlTUB.so is not a shared library; it's an executable. You can't link against an executable. Probably whatever build process you used for libxrlTUB.so didn't understand how to build shared libraries on the Mac (it's more suspect because .dylib is the correct extension to use.)
Take a look at Apple's documentation on compiling dynamic libraries. You can use file to make sure your output is of the correct type, for example:
% gcc -c foo.c
% gcc -dynamiclib foo.o -o foo.dylib
% file foo.dylib
foo.dylib: Mach-O 64-bit dynamically linked shared library x86_64
Without -dynamiclib you end up with an executable, which may be the problem you've run into.