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

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

Related

R install packages looking in wrong place

Trying to install FFTW and feel like I have been going in circles. I need it for an R package (poisbinom) and I think I have it installed right (I'm on a cluster and don't have sudo privileges. I followed the instructions here: http://micro.stanford.edu/wiki/Install_FFTW3 )
The problem I'm running into is this:
g++ -std=gnu++14 -shared -L/usr/local/lib64 -o poisbinom.so RcppExports.o init.o poisbinom.o -lfftw3 -lm
/usr/bin/ld: cannot find -lfftw3
collect2: error: ld returned 1 exit status
I have a feeling it is because I did not install FFTW under usr/local/lib64, its under $HOME/usr/. My R install is under $HOME/R/lib64/R. I am not sure what variable to change to make it look in the right place, as I cant change this g++ call as it is part of an R install.packages call. I've been going in circles with this for two days now. Let me know if theres anything else I can provide to help.
I already ran install.packages("fftw") and that was successful and used LFFTW3 without issue because it looked in the right place. I mostly just need to know if I can point this gcc call to the right folder with an environment variable or something.
In case you have pkg-config on your system and your installation produced the required configuration files (e.g. fftw3.pc): Define the environment variable PKG_CONFIG_PATH to (also) include the directory with these configuration files for FFTW3.
Otherwise, you can define FFTW_CFLAGS and FFTW_LIBS:
fftw$ ./configure --help
[...]
FFTW_CFLAGS C compiler flags for FFTW, overriding pkg-config
FFTW_LIBS linker flags for FFTW, overriding pkg-config
[...]
I ended up having to add the variables to my bashrc file:
export PATH=$HOME/fftw_folder/bin:$PATH
export LD_LIBRARY_PATH=$HOME/fftw_folder/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=$HOME/fftw_folder/lib:$LIBRARY_PATH
export PKG_CONFIG_PATH=$HOME/_folder/lib/pkgconfig:$PKG_CONFIG_PATH
As laid out here: http://hpc.loni.org/docs/faq/installation-details.php
And now things seem to be moving forward smoothly. Thanks for the help!

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.

/usr/bin/ld cannot find -l<nameOfLibrary>

I have written a few programs and while trying to compile them using g++,as thus,
$ g++ minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp -L/usr/local/lib/ -lfwlib32 -lpthread -o adapter
I keep getting the following error:
/usr/bin/ld: cannot find -lfwlib32
collect2: error: ld returned 1 exit status
fwlib32.h is the library I am trying to include. The shared object file libfwlib32.so is present in /usr/local/lib as well as /usr/lib. But I am unable link to it. I have tried all the solutions offered by similar questions including
$ export LIBRARY_PATH=/usr/local/lib/
$ export LD_LIBRARY_PATH=/usr/local/lib
I have done the above for /usr/lib as well, but still the same error.
I have tried using the -L option in the command line but I still get the error.
I even created a new folder called lib, pasted libfwlib32.so.1.0.1 into it and ran
$ ln -s ~/lib/libfwlib32.so.1.0.1 ~/lib/libfwlib32.so
on the console to create a new .so file and gave ~/lib as argument to -L option on the command line. It made no difference. I am at the point of tearing my hair out so any help will be appreciated.
Thanks alot!
You should put -l option in the very last as:
$ g++ minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp -L/usr/local/lib/ -o adapter -lfwlib32 -lpthread
Note: Please make sure that all the header and source file are in the same folder.
Note that specifying -L~/lib won't work as the ~ will not be expanded by the shell. Also you can't add a space between -L and ~/lib. Instead you must specify it as a relative or absolute path.
Have you checked that the libfwlib32.so symlink exists in /usr/local/lib (or /usr/lib) in addition to the libfwlib32.so.1.0.1 file?
Another possibility is that the library is the wrong architecture (ie. 32-bit while your system is 64-bit), but then ld should print a message about skipping incompatible library. You can check the architecture of the library by running 'file libfwlib32.so.1.0.1'.
The error message suggests that -lfwlib32 is being interpreted as a filename not as a -l parameter. Put all the parameters before the files to be compiled
g++ -m32 -L/usr/local/lib/ -lfwlib32 -lpthread -o adapter minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp
As has been pointed out by #Erik Johannessen, libfwlib32.so is a 32bit library, so you need to add -m32 to build a 32bit executable.

gcc fails to find a library to link

I am using Ubuntu and my IDE is Aptana 3.0
I am getting the following error when trying to build. The library libfcgi.a is located in /usr/local/lib/.
In the Library C++ Linker section of the project properties, I added /usr/local/lib/ to the search path and the file /usr/local/lib/libfcgi.a.
Why can I still not build?
**** Build of configuration Debug for project rtb ****
make all
Building target: rtb
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o"rtb" ./src/rtb.o -l/usr/local/lib/libfcgi.a
/usr/bin/ld: cannot find -l/usr/local/lib/libfcgi.a
collect2: ld returned 1 exit status
make: *** [rtb] Error 1
I think this may be what you want;
g++ -L/usr/local/lib -o "rtb" ./src/rtb.o -lfcgi
-l<value> will automatically look in all folders listed with -L for a library named lib<value>.a or lib<value>.so, all you need is the '-lfcgi'.
Try "-lfcgi" instead
g++ -L/usr/local/lib -o"rtb" ./src/rtb.o -lfcgi
Your linker flag is wrong it needs to be -lfcgi and not the whole path with the "lib" prefix and the .a suffix.
You can change it by hand, or in Apatana. To do so you don't have to give him the full qualified path to your lib two times. ( You already gave him the search path, remember? ). Usually you define additional libraries just like this:
cfgi and your IDE does the rest to add it to the linker flags!
You should do
g++ -o "rtb" ./src/rtb.o -lfcgi
In the unlikely case that /usr/local/lib/ is not in your search path, you can either add that path in the command line like
g++ -L/usr/local/lib -o "rtb" ./src/rtb.o -lfcgi
or put it in the environment variable LIBRARY_PATH before calling your compile command, e.g. with bash:
if [ -z "$LIBRARY_PATH" ];
then export LIBRARY_PATH=/usr/local/lib;
else export LIBRARY_PATH="$LIBRARY_PATH":/usr/local/lib;
fi
If you insist in giving the explicit file name, omit the -l:
g++ -L/usr/local/lib -o "rtb" ./src/rtb.o /usr/local/lib/libfcgi.a
However I'd advise against that because it's inflexible. If the issue is that there's another, incompatible version of the library installed, it's better to make sure that the correct one comes first in the search path (or even better, make sure that the wrong one isn't in the search path at all ― maybe even by removing it from the system).
That's for the help all....really appreciate it.
Here is the solution. I had to use -lfcgi++ and I added the
g++ -L/usr/local/include/ -lfcgi++ -o"rtb" ./src/rtb.o
The above was the output from aptanta console. I tried -lfcgi and did not work. Google search lead to using -lfcgi++.

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.