int f ();
#include "test.h"
int f ()
{
return 3;
}
/*
gcc -c test.c
ar cr libtest.a test.o
gcc -c -fPIC test.c
gcc -shared -fPIC test.o -o libtest.so
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include "test.h"
int main ()
{
return f ();
}
/*
gcc -c test.c
gcc -c app.c
ar cr libtest.a test.o
gcc -L. -ltest app.o -o app
/usr/bin/ld: app.o: in function `main':
app.c:(.text+0xe): undefined reference to `f'
collect2: error: ld returned 1 exit status
gcc -static app.o -L. -ltest -o app
./app
echo $?
3
gcc app.o -L. -ltest -o app // use shared library
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
echo $LD_LIBRARY_PATH
:.
./app
echo $?
3
gcc app.o -L. -ltest -Wl,-rpath,. -o app
./app
echo $?
3
ldd app
libtest.so...
libc...
rm *.o // clean
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
P3
# "P3" means this is a RGB color image in ASCII
# "3 2" is the width and height of the image in pixels
# "255" is the maximum value for each color
# This, up through the "255" line below are the header.
# Everything after that is the image data: RGB triplets.
# In order: red, green, blue, yellow, white, and black.
3 2
255
255 0 0
0 255 0
0 0 255
255 255 0
255 255 255
0 0 0
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // printf(), fprintf(), stderr()
#include <tiffio.h> // for TIFF, TIFFOpen(), TIFFClose(), TIFFGetField
// /usr/include/x86_64-linux-gnu/tiffio.h includes
// /usr/include/x86_64-linux-gnu/tiff.h, which defines:
// TIFFTAG_IMAGEWIDTH, TIFFTAG_IMAGELENGTH,
// TIFFTAG_XRESOLUTION, TIFFTAG_YRESOLUTION
int main (int argc, char** argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./tifftest image.tiff\n");
return 1; // end program, signalling error
}
// else
int w, h;
float xdpi, ydpi;
TIFF *tiff;
tiff = TIFFOpen (argv[1], "r"); // open for reading
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(tiff, , &xdpi);
TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &ydpi);
TIFFClose (tiff);
printf("Image width: %d\n", w);
printf("Image hight: %d\n", h);
printf("X Resolution: %g dpi\n", xdpi);
printf("Y Resolution: %g dpi\n", ydpi);
return 0;
}
/*
sudo apt-get install zlib1g-dev libpng-dev libpng++-dev libtiff5-dev
gcc tifftest.c -static -ltiff -o tifftest
// unresolved symbols
gcc tifftest.c -ltiff -o tifftest // use shared library
./tifftest
Usage: ./tifftest image.tiff
ldd tifftest
libc.so...
libjpeg.so...
libz.so...
libm.so...
./tifftest image.tiff
Image width: 3
Image hight: 2
X Resolution: 72 dpi
Y Resolution: 72 dpi
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
int f ();
#include "test.h"
int f()
{
return 3;
}
/*
gcc -c -fPIC test.c
gcc -shared -fPIC test.o -o libtest.so
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <assert.h> // for assert()
#include <stdio.h> // printf(), fprintf(), stderr, NULL
#include <stdlib.h> // for exit(), EXIT_FAILURE, EXIT_SUCCESS
#include <dlfcn.h> // for dlopen(), dlerror(), dlsym(), dlclose()
// dlfcn.h includes bits/dlfcn.h, which defines RTLD_LAZY:
// /usr/include/x86_64-linux-gnu/bits/dlfcn.h:
// #define RTLD_LAZY 0x00001 /* Lazy function call binding. */
/*
extern void *dlopen (const char *file, int mode);
extern char *dlerror (void);
extern void *dlsym (void *handle, const char *name);
extern int dlclose (void *handle);
*/
#include "test.h" // for f()
int main() // run-time dynamic loading
{
void *handle = NULL; // initialize
int (*local_f)(void);
char *error = NULL;
handle = dlopen("libtest.so", RTLD_LAZY);
if (!handle)
{
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
local_f = (int (*)(void)) dlsym(handle, "f");
error = dlerror();
if (error != NULL)
{
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
// else
assert(local_f() == 3);
printf("f() = %d\n", local_f());
dlclose(handle);
return EXIT_SUCCESS;
}
/*
gcc -c -fPIC test.c
gcc -shared -fPIC test.o -o libtest.so
gcc -c app.c
gcc app.o -o app -ldl
// We did not link libtest.so, but libdl.so
ldd app
libdl.so...
libc...
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
echo $LD_LIBRARY_PATH
:.
./app
f() = 3
rm *.o // clean
*/