How to properly call C++ library function in Java Activity - c++

I am using GitHub in-paint library https://github.com/cheind/inpaint in android studio
and trying to call jni fuction of inpainter in main activity but it is not resolving or getting recognized.
C++ is new to me so i am having issue.
in criminisi_inpainter.cpp file
there is this function
void inpaintCriminisi(
cv::InputArray image,
cv::InputArray targetMask,
cv::InputArray sourceMask,
int patchSize)
{
CriminisiInpainter ci;
ci.setSourceImage(image.getMat());
ci.setSourceMask(sourceMask.getMat());
ci.setTargetMask(targetMask.getMat());
ci.setPatchSize(patchSize);
ci.initialize();
while (ci.hasMoreSteps()) {
ci.step();
}
ci.image().copyTo(image.getMat());
}
this native function i have to call in main activity
public native void inpaintCriminisi( ? , ? ,? , int z);
The issue is i do not understand what parameters would be provided here in main activity (JAVA)
In nativelib.cpp i am exporting jnicall function i give object parameters which is not exact parameters that should be defined.
extern "C"
JNIEXPORT void JNICALL
Java_com_example_raweeda_applicationc_MainActivity_inpaintCriminisi(JNIEnv *env, jobject instance,
jobject a, jobject b, jobject c,
jint xl) {
// TODO
inpaintCriminisi(x,y,z,xl);
parameters are not object it just an example
}
this is the error i am recieving
Build command failed.
Error while executing process C:\Users\abc\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\2019_android\ApplicationC\app\.externalNativeBuild\cmake\debug\x86 --target native-lib}
[1/3] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/3] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/criminisi_inpainter.cpp.o
FAILED: C:\Users\abc\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android19 --gcc-toolchain=C:/Users/abc/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 -Dnative_lib_EXPORTS -ID:/opencv-sdk/OpenCV-android-sdk/sdk/native/jni/include --sysroot C:/Users/abc/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -std=c++11 -frtti -fexceptions -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -MF CMakeFiles\native-lib.dir\src\main\cpp\native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -c D:\2019_android\ApplicationC\app\src\main\cpp\native-lib.cpp
D:\2019_android\ApplicationC\app\src\main\cpp\native-lib.cpp:13:5: warning: declaration does not declare anything [-Wmissing-declarations]
cv::InputArray ;
^~~~~~~~~~~~~~
D:\2019_android\ApplicationC\app\src\main\cpp\native-lib.cpp:23:39: error: expected '(' for function-style cast or type construction
inpaintCriminisi( cv::InputArray image,
~~~~~~~~~~~~~~ ^
D:\2019_android\ApplicationC\app\src\main\cpp\native-lib.cpp:24:28: error: expected '(' for function-style cast or type construction
cv::InputArray targetMask,
~~~~~~~~~~~~~~ ^
D:\2019_android\ApplicationC\app\src\main\cpp\native-lib.cpp:25:28: error: expected '(' for function-style cast or type construction
cv::InputArray sourceMask,
~~~~~~~~~~~~~~ ^
D:\2019_android\ApplicationC\app\src\main\cpp\native-lib.cpp:26:17: error: expected '(' for function-style cast or type construction
int patchSize);
~~~ ^
1 warning and 4 errors generated.
FAILED: C:\Users\abc\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android19 --gcc-toolchain=C:/Users/abc/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 -Dnative_lib_EXPORTS -ID:/opencv-sdk/OpenCV-android-sdk/sdk/native/jni/include --sysroot C:/Users/abc/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -std=c++11 -frtti -fexceptions -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/criminisi_inpainter.cpp.o -MF CMakeFiles\native-lib.dir\src\main\cpp\criminisi_inpainter.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/criminisi_inpainter.cpp.o -c D:\2019_android\ApplicationC\app\src\main\cpp\criminisi_inpainter.cpp
In file included from D:\2019_android\ApplicationC\app\src\main\cpp\criminisi_inpainter.cpp:7:
D:\2019_android\ApplicationC\app\src\main\cpp/timer.h:83:1: error: extraneous closing brace ('}')
}
^
D:\2019_android\ApplicationC\app\src\main\cpp\criminisi_inpainter.cpp:10:10: fatal error: 'opencv' file not found
#include "opencv"
^~~~~~~~
2 errors generated.
ninja: build stopped: subcommand failed.
because in nativelib.cpp

Related

Cygwin cmake problems on windows

I am trying to cmake on windows computer. I encountered following error in CMakeError.log
Run Build Command(s):/usr/bin/ninja.exe cmTC_ff288 && [1/2] Building C object CMakeFiles/cmTC_ff288.dir/CheckSymbolExists.c.o
FAILED: CMakeFiles/cmTC_ff288.dir/CheckSymbolExists.c.o
/usr/bin/cc -fdiagnostics-color=always -Wextra -Wall -Wno-expansion-to-defined -Wno-psabi -Wno-misleading-indentation -Wno-maybe-uninitialized -Wwrite-strings -Wundef -Wpointer-arith -Wmissing-format-attribute -Wformat-security -Wcast-align -Wno-tautological-compare -fno-strict-aliasing -fno-exceptions -o CMakeFiles/cmTC_ff288.dir/CheckSymbolExists.c.o -c CheckSymbolExists.c
CheckSymbolExists.c:2:10: fatal error: pthread_np.h: No such file or directory
2 | #include <pthread_np.h>
| ^~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
File /cygdrive/f/gtkwebkit/webkitgtk-2.34.1/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread_np.h>
int main(int argc, char** argv)
{
(void)argv;
#ifndef pthread_main_np
return ((int*)(&pthread_main_np))[argc];
#else
(void)argc;
return 0;
#endif
}
Does anyone know how to fix it? Since it is a temp file I could not modify this line of include

LLVM build using cmake fails during tsan_libdispatch_mac.cc; "Error: conflicting types for ..."

I have at least successfully configured the build for the llvm compiler according to the directions here.
When I go to build using cmake --build . -v I get the following error output:
$ cmake --build . -v
...
[ 63%] Building CXX object projects/compiler-rt/lib/tsan/CMakeFiles/clang_rt.tsan_osx_dynamic.dir/rtl/tsan_libdispatch_mac.cc.o
cd /Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/build/projects/compiler-rt/lib/tsan && /usr/bin/clang++ --sysroot=/ -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Dclang_rt_tsan_osx_dynamic_EXPORTS -I/Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/build/projects/compiler-rt/lib/tsan -I/Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/projects/compiler-rt/lib/tsan -I/Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/build/include -I/Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/include -I/Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/projects/compiler-rt/lib/tsan/.. -isystem / -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wcovered-switch-default -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -std=c++11 -Wall -std=c++11 -O3 -DNDEBUG -arch x86_64 -arch x86_64h -fPIC -stdlib=libc++ -mmacosx-version-min=10.9 -fPIC -fno-builtin -fno-exceptions -fomit-frame-pointer -funwind-tables -fno-stack-protector -fno-sanitize=safe-stack -fvisibility=hidden -fvisibility-inlines-hidden -fno-function-sections -fno-lto -O3 -gline-tables-only -Wno-gnu -Wno-variadic-macros -Wno-c99-extensions -Wno-non-virtual-dtor -fPIE -fno-rtti -msse3 -Wframe-larger-than=512 -Wglobal-constructors -o CMakeFiles/clang_rt.tsan_osx_dynamic.dir/rtl/tsan_libdispatch_mac.cc.o -c /Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc
/Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc:144:22: error:
conflicting types for 'dispatch_sync'
DISPATCH_INTERCEPT_B(dispatch_sync)
^
/usr/include/dispatch/queue.h:292:1: note: previous declaration is here
dispatch_sync(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block);
^
/Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc:145:22: error:
conflicting types for 'dispatch_barrier_sync'
DISPATCH_INTERCEPT_B(dispatch_barrier_sync)
^
/usr/include/dispatch/queue.h:1278:1: note: previous declaration is here
dispatch_barrier_sync(dispatch_queue_t queue,
^
/Users/anthonymcknight/Documents/cubing/bfs/lab4/cilk/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc:162:24: error:
conflicting types for 'dispatch_once'
TSAN_INTERCEPTOR(void, dispatch_once, dispatch_once_t *predicate,
^
/usr/include/dispatch/once.h:73:1: note: previous declaration is here
dispatch_once(dispatch_once_t *predicate,
^
3 errors generated.
make[2]: *** [projects/compiler-rt/lib/tsan/CMakeFiles/clang_rt.tsan_osx_dynamic.dir/rtl/tsan_libdispatch_mac.cc.o] Error 1
make[1]: *** [projects/compiler-rt/lib/tsan/CMakeFiles/clang_rt.tsan_osx_dynamic.dir/all] Error 2
make: *** [all] Error 2
I was previously able to workaround buggy compiler flags set by cmake by simply copying and pasting the verbose output and correcting it in the command line, but I'm not sure what to edit here to get things to work.
I've tried changing -isysroot, copying and pasting header files into llvm/include from /usr/include or the MacOSX10.14.sdk file, and the persistent issue is the error: conflicting types ...
I've even tried doing small modifications to tsan_libdispatch_mac.cc but none of them worked (e.g., changing #include <dispatch/dispatch.h> to #include <dispatch.h>, changing "user_alloc" to "user_alloc_internal"...).
Any suggestions are welcome.
Anthony
Basically I had to replace my original tsan_libdispatch_mac.cc with the one form here. I have yet to complete the full installation, much less test performance, but my build is proceeding past this point now.
I tried to compile it for llvm 3.9.1 but the tsan_libdispatch_mac.cc is different. Here's a diff that works:
diff --git a/lib/tsan/rtl/tsan_libdispatch_mac.cc b/lib/tsan/rtl/tsan_libdispatch_mac.cc
index 529cedba4..86982e361 100644
--- a/lib/tsan/rtl/tsan_libdispatch_mac.cc
+++ b/lib/tsan/rtl/tsan_libdispatch_mac.cc
## -144,7 +144,7 ## static void invoke_and_release_block(void *param) {
}
#define DISPATCH_INTERCEPT_SYNC_B(name, barrier) \
- TSAN_INTERCEPTOR(void, name, dispatch_queue_t q, dispatch_block_t block) { \
+ TSAN_INTERCEPTOR(void, name, dispatch_queue_t q, DISPATCH_NOESCAPE dispatch_block_t block) { \
SCOPED_TSAN_INTERCEPTOR(name, q, block); \
SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); \
dispatch_block_t heap_block = Block_copy(block); \
## -234,7 +234,7 ## TSAN_INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when,
// need to undefine the macro.
#undef dispatch_once
TSAN_INTERCEPTOR(void, dispatch_once, dispatch_once_t *predicate,
- dispatch_block_t block) {
+ DISPATCH_NOESCAPE dispatch_block_t block) {
SCOPED_INTERCEPTOR_RAW(dispatch_once, predicate, block);
atomic_uint32_t *a = reinterpret_cast<atomic_uint32_t *>(predicate);
u32 v = atomic_load(a, memory_order_acquire);
## -444,7 +444,7 ## TSAN_INTERCEPTOR(void, dispatch_source_set_registration_handler_f,
}
TSAN_INTERCEPTOR(void, dispatch_apply, size_t iterations,
- dispatch_queue_t queue, void (^block)(size_t)) {
+ dispatch_queue_t queue, DISPATCH_NOESCAPE void (^block)(size_t)) {
SCOPED_TSAN_INTERCEPTOR(dispatch_apply, iterations, queue, block);
void *parent_to_child_sync = nullptr;

Build of nixos.avrgcclibc fails on NixOS

I am trying to install avr-gcc-libc on NixOS, but run into a build error.
I try to install with the command
nix-env -iA nixos.avrgcclibc
The output this produces is very long and terminates with
g++ -c -DIN_GCC_FRONTEND -g -O2 -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -DHAVE_CONFIG_H -I. -Icp -I../../gcc -I../../gcc/cp -I../../gcc/../include -I../../gcc/../libcpp/include -I../../gcc/../libdecnumber -I../../gcc/../libdecnumber/dpd -I../libdecnumber -I../../gcc/../libbacktrace -o cp/except.o
-MT cp/except.o -MMD -MP -MF cp/.deps/except.TPo ../../gcc/cp/except.c
In file included from ./tm.h:18:0,
from ../../gcc/cp/except.c:27:
../../gcc/config/elfos.h:102:21: warning: invalid suffix on literal; C++11 requires a space between literal and string macro [-Wliteral-suffix]
fprintf ((FILE), "%s"HOST_WIDE_INT_PRINT_UNSIGNED"\n",\
^
../../gcc/config/elfos.h:170:24: warning: invalid suffix on literal; C++11 requires a space between literal and string macro [-Wliteral-suffix]
fprintf ((FILE), ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", \
^
In file included from ../../gcc/cp/except.c:1023:0:
cfns.gperf: In function 'const char* libc_name_p(const char*, unsigned int)':
cfns.gperf:101:1: error: 'const char* libc_name_p(const char*, unsigned int)' redeclared inline with 'gnu_inline' attribute
cfns.gperf:26:14: note: 'const char* libc_name_p(const char*, unsigned int)' previously declared here
cfns.gperf: At global scope:
cfns.gperf:26:14: warning: inline function 'const char* libc_name_p(const char*, unsigned int)' used but never defined
make[2]: *** [Makefile:1064: cp/except.o] Error 1
make[2]: Leaving directory '/tmp/nix-build-avr-gcc-libc.drv-0/gcc-5.3.0/obj-avr/gcc'
make[1]: *** [Makefile:4094: all-gcc] Error 2
make[1]: Leaving directory '/tmp/nix-build-avr-gcc-libc.drv-0/gcc-5.3.0/obj-avr'
make: *** [Makefile:852: all] Error 2
builder for ‘/nix/store/0ypaa4x9x0lsyygciasw9m1v4jswgm49-avr-gcc-libc.drv’ failed with exit code 2
error: build of ‘/nix/store/0ypaa4x9x0lsyygciasw9m1v4jswgm49-avr-gcc-libc.drv’ failed
What might the problem be? What can I do to get avr-gcc-libc to install successfully?

getting error : ‘Foam::solverPersistentData<Foam::lduMesh, Foam::GPUSolverData>::typeName’ is ambiguous

while executing ./Allwmake getting the ambiguous error. error is given below.
Found nvcc -- enabling CUDA support.
SOURCE=lduMatrixConversion/lduMatrixConversion.C ; g++ -m64 -Dlinux64 -DWM_DP -Wall -Wextra -Wno-unused-parameter -Wold-style-cast -O3 -DNoRepository -ftemplate-depth-100 -I/home/suraj/ispm-sparse-lib-master -isystem /usr/local/cuda-7.5/include -IlnInclude -I. -I/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude -I/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OSspecific/POSIX/lnInclude -fPIC -c $SOURCE -o Make/linux64Gcc48DPOpt/lduMatrixConversion.o
SOURCE=GPUPCG/GPUPCG.C ; g++ -m64 -Dlinux64 -DWM_DP -Wall -Wextra -Wno-unused-parameter -Wold-style-cast -O3 -DNoRepository -ftemplate-depth-100 -I/home/suraj/ispm-sparse-lib-master -isystem /usr/local/cuda-7.5/include -IlnInclude -I. -I/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude -I/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OSspecific/POSIX/lnInclude -fPIC -c $SOURCE -o Make/linux64Gcc48DPOpt/GPUPCG.o
SOURCE=AsyncPrecondProvider/AsyncPrecondProvider.C ; g++ -m64 -Dlinux64 -DWM_DP -Wall -Wextra -Wno-unused-parameter -Wold-style-cast -O3 -DNoRepository -ftemplate-depth-100 -I/home/suraj/ispm-sparse-lib-master -isystem /usr/local/cuda-7.5/include -IlnInclude -I. -I/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude -I/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OSspecific/POSIX/lnInclude -fPIC -c $SOURCE -o Make/linux64Gcc48DPOpt/AsyncPrecondProvider.o
SOURCE=GPUSolverData/GPUSolverData.C ; g++ -m64 -Dlinux64 -DWM_DP -Wall -Wextra -Wno-unused-parameter -Wold-style-cast -O3 -DNoRepository -ftemplate-depth-100 -I/home/suraj/ispm-sparse-lib-master -isystem /usr/local/cuda-7.5/include -IlnInclude -I. -I/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude -I/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OSspecific/POSIX/lnInclude -fPIC -c $SOURCE -o Make/linux64Gcc48DPOpt/GPUSolverData.o
In file included from /home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/MeshObject.H:281,
from lnInclude/solverPersistentData.H:43,
from GPUPCG/GPUPCG.H:43,
from GPUPCG/GPUPCG.C:32:
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/MeshObject.C: In static member function ‘static const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New(const Mesh&) [with Mesh = Foam::lduMesh, MeshObjectType = Foam::UpdateableMeshObject, Type = Foam::solverPersistentData<Foam::lduMesh, Foam::GPUSolverData>]’:
GPUPCG/GPUPCG.C:65: instantiated from here
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/MeshObject.C:48: error: reference to ‘Foam::solverPersistentData<Foam::lduMesh, Foam::GPUSolverData>::typeName’ is ambiguous
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOobject.H:177: error: candidates are: const Foam::word Foam::IOobject::typeName
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/regIOobject.H:125: error: const Foam::word Foam::regIOobject::typeName
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/MeshObject.H:168: error: const Foam::word Foam::meshObject::typeName
lnInclude/solverPersistentData.H:66: error: const Foam::word Foam::solverPersistentDataName::typeName
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/MeshObject.C:59: error: reference to ‘Foam::solverPersistentData<Foam::lduMesh, Foam::GPUSolverData>::typeName’ is ambiguous
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOobject.H:177: error: candidates are: const Foam::word Foam::IOobject::typeName
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/regIOobject.H:125: error: const Foam::word Foam::regIOobject::typeName
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/MeshObject.H:168: error: const Foam::word Foam::meshObject::typeName
lnInclude/solverPersistentData.H:66: error: const Foam::word Foam::solverPersistentDataName::typeName
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/MeshObject.C:65: error: reference to ‘Foam::solverPersistentData<Foam::lduMesh, Foam::GPUSolverData>::typeName’ is ambiguous
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOobject.H:177: error: candidates are: const Foam::word Foam::IOobject::typeName
/home/suraj/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/regIOobject.H:125: error: const Foam::word Foam::regIOobject::typeName
and most of the errors are due to ambiguity in the code.
the files are used from OpenFOAM2.4.x distriution. which are available online.

Undefined reference with Eclipse for source in different directory

I have an old source which I need to integrate in my project in Eclipse (in Ubuntu 12.04).
The old source code works perfectly on its own. But the only problem with it is - I have just one .cpp file (which contains the main) and all others are .h files which contains the definitions and declarations. I fixed this by creating a source file for each header file and copying the function definitions in these files and left the declarations in the header files.
I added these files using eclipse so those files actually are getting compiled.
Note that, I have added this source code into "src/segment". Here is my directory listing. These files also show up in the eclipse project. My main function is in "HelloOpenCV2.cpp"
[eeuser#roadrunner src]$ ls
CameraPoseFromFundamentalMatrix.d InterestPoints.o
CameraPoseFromFundamentalMatrix.o LinesAndComponents.d
GLViz.d LinesAndComponents.o
GLViz.o MorphologicalManip.d
GraphImageSegmentation.d MorphologicalManip.o
GraphImageSegmentation.o MultiviewColorConsistency.d
HelloOpenCV2.d MultiviewColorConsistency.o
HelloOpenCV2.o segment
HomographyWarp.d StereoCamDepth.d
HomographyWarp.o StereoCamDepth.o
ImageManipulations.d subdir.mk
ImageManipulations.o VideoProc.d
InterestPoints.d VideoProc.o
[eeuser#roadrunner src]$ ls segment/
convolve.d filter.d imconv.d segment-graph.d subdir.mk
convolve.o filter.o imconv.o segment-graph.o
disjoint-set.d image.d imutil.d segment-image.d
disjoint-set.o image.o imutil.o segment-image.o
Here is my compilation log as generated by eclipse.
18:54:21 **** Build of configuration Debug for project HelloOpenCV2 ****
make all
Building file: ../src/segment/convolve.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/convolve.d" -MT"src/segment/convolve.d" -o "src/segment/convolve.o" "../src/segment/convolve.cpp"
Finished building: ../src/segment/convolve.cpp
Building file: ../src/segment/disjoint-set.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/disjoint-set.d" -MT"src/segment/disjoint-set.d" -o "src/segment/disjoint-set.o" "../src/segment/disjoint-set.cpp"
Finished building: ../src/segment/disjoint-set.cpp
Building file: ../src/segment/filter.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/filter.d" -MT"src/segment/filter.d" -o "src/segment/filter.o" "../src/segment/filter.cpp"
Finished building: ../src/segment/filter.cpp
Building file: ../src/segment/image.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/image.d" -MT"src/segment/image.d" -o "src/segment/image.o" "../src/segment/image.cpp"
Finished building: ../src/segment/image.cpp
Building file: ../src/segment/imconv.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/imconv.d" -MT"src/segment/imconv.d" -o "src/segment/imconv.o" "../src/segment/imconv.cpp"
Finished building: ../src/segment/imconv.cpp
Building file: ../src/segment/imutil.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/imutil.d" -MT"src/segment/imutil.d" -o "src/segment/imutil.o" "../src/segment/imutil.cpp"
Finished building: ../src/segment/imutil.cpp
Building file: ../src/segment/segment-graph.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/segment-graph.d" -MT"src/segment/segment-graph.d" -o "src/segment/segment-graph.o" "../src/segment/segment-graph.cpp"
Finished building: ../src/segment/segment-graph.cpp
Building file: ../src/segment/segment-image.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/segment-image.d" -MT"src/segment/segment-image.d" -o "src/segment/segment-image.o" "../src/segment/segment-image.cpp"
Finished building: ../src/segment/segment-image.cpp
Building file: ../src/CameraPoseFromFundamentalMatrix.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/CameraPoseFromFundamentalMatrix.d" -MT"src/CameraPoseFromFundamentalMatrix.d" -o "src/CameraPoseFromFundamentalMatrix.o" "../src/CameraPoseFromFundamentalMatrix.cpp"
Finished building: ../src/CameraPoseFromFundamentalMatrix.cpp
Building file: ../src/GLViz.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/GLViz.d" -MT"src/GLViz.d" -o "src/GLViz.o" "../src/GLViz.cpp"
Finished building: ../src/GLViz.cpp
Building file: ../src/GraphImageSegmentation.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/GraphImageSegmentation.d" -MT"src/GraphImageSegmentation.d" -o "src/GraphImageSegmentation.o" "../src/GraphImageSegmentation.cpp"
Finished building: ../src/GraphImageSegmentation.cpp
Building file: ../src/HelloOpenCV2.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/HelloOpenCV2.d" -MT"src/HelloOpenCV2.d" -o "src/HelloOpenCV2.o" "../src/HelloOpenCV2.cpp"
Finished building: ../src/HelloOpenCV2.cpp
Building file: ../src/HomographyWarp.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/HomographyWarp.d" -MT"src/HomographyWarp.d" -o "src/HomographyWarp.o" "../src/HomographyWarp.cpp"
Finished building: ../src/HomographyWarp.cpp
Building file: ../src/ImageManipulations.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/ImageManipulations.d" -MT"src/ImageManipulations.d" -o "src/ImageManipulations.o" "../src/ImageManipulations.cpp"
Finished building: ../src/ImageManipulations.cpp
Building file: ../src/InterestPoints.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/InterestPoints.d" -MT"src/InterestPoints.d" -o "src/InterestPoints.o" "../src/InterestPoints.cpp"
Finished building: ../src/InterestPoints.cpp
Building file: ../src/LinesAndComponents.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/LinesAndComponents.d" -MT"src/LinesAndComponents.d" -o "src/LinesAndComponents.o" "../src/LinesAndComponents.cpp"
Finished building: ../src/LinesAndComponents.cpp
Building file: ../src/MorphologicalManip.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/MorphologicalManip.d" -MT"src/MorphologicalManip.d" -o "src/MorphologicalManip.o" "../src/MorphologicalManip.cpp"
Finished building: ../src/MorphologicalManip.cpp
Building file: ../src/MultiviewColorConsistency.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/MultiviewColorConsistency.d" -MT"src/MultiviewColorConsistency.d" -o "src/MultiviewColorConsistency.o" "../src/MultiviewColorConsistency.cpp"
Finished building: ../src/MultiviewColorConsistency.cpp
Building file: ../src/StereoCamDepth.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/StereoCamDepth.d" -MT"src/StereoCamDepth.d" -o "src/StereoCamDepth.o" "../src/StereoCamDepth.cpp"
Finished building: ../src/StereoCamDepth.cpp
Building file: ../src/VideoProc.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/VideoProc.d" -MT"src/VideoProc.d" -o "src/VideoProc.o" "../src/VideoProc.cpp"
Finished building: ../src/VideoProc.cpp
Building target: HelloOpenCV2
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "HelloOpenCV2" ./src/segment/convolve.o ./src/segment/disjoint-set.o ./src/segment/filter.o ./src/segment/image.o ./src/segment/imconv.o ./src/segment/imutil.o ./src/segment/segment-graph.o ./src/segment/segment-image.o ./src/CameraPoseFromFundamentalMatrix.o ./src/GLViz.o ./src/GraphImageSegmentation.o ./src/HelloOpenCV2.o ./src/HomographyWarp.o ./src/ImageManipulations.o ./src/InterestPoints.o ./src/LinesAndComponents.o ./src/MorphologicalManip.o ./src/MultiviewColorConsistency.o ./src/StereoCamDepth.o ./src/VideoProc.o -lopencv_core -lGL -lGLU -lglut -lopencv_stitching -lopencv_contrib -lopencv_nonfree -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_flann -lopencv_imgproc -lopencv_highgui
../src/segment/filter.cpp:39: error: undefined reference to 'image<float>::image(int, int, bool)'
../src/segment/filter.cpp:40: error: undefined reference to 'image<float>::image(int, int, bool)'
../src/segment/filter.cpp:41: error: undefined reference to 'convolve_even(image<float>*, image<float>*, std::vector<float, std::allocator<float> >&)'
../src/segment/filter.cpp:42: error: undefined reference to 'convolve_even(image<float>*, image<float>*, std::vector<float, std::allocator<float> >&)'
../src/segment/filter.cpp:44: error: undefined reference to 'image<float>::~image()'
../src/segment/filter.cpp:52: error: undefined reference to 'imageUCHARtoFLOAT(image<unsigned char>*)'
../src/segment/filter.cpp:54: error: undefined reference to 'image<float>::~image()'
../src/segment/filter.cpp:63: error: undefined reference to 'image<float>::image(int, int, bool)'
../src/segment/imconv.cpp:15: error: undefined reference to 'image<unsigned char>::image(int, int, bool)'
../src/segment/imconv.cpp:31: error: undefined reference to 'image<rgb>::image(int, int, bool)'
../src/segment/imconv.cpp:46: error: undefined reference to 'image<float>::image(int, int, bool)'
../src/segment/imconv.cpp:73: error: undefined reference to 'image<unsigned char>::image(int, int, bool)'
../src/segment/imconv.cpp:90: error: undefined reference to 'void min_max<float>(image<float>*, float*, float*)'
../src/segment/imconv.cpp:97: error: undefined reference to 'image<long>::image(int, int, bool)'
../src/segment/imconv.cpp:110: error: undefined reference to 'image<unsigned char>::image(int, int, bool)'
../src/segment/imconv.cpp:127: error: undefined reference to 'void min_max<long>(image<long>*, long*, long*)'
../src/segment/imconv.cpp:135: error: undefined reference to 'image<unsigned char>::image(int, int, bool)'
../src/segment/imconv.cpp:152: error: undefined reference to 'void min_max<short>(image<short>*, short*, short*)'
../src/segment/segment-image.cpp:62: error: undefined reference to 'smooth(image<float>*, float)'
../src/segment/segment-image.cpp:63: error: undefined reference to 'smooth(image<float>*, float)'
../src/segment/segment-image.cpp:64: error: undefined reference to 'smooth(image<float>*, float)'
../src/segment/segment-image.cpp:65: error: undefined reference to 'image<float>::~image()'
../src/segment/segment-image.cpp:66: error: undefined reference to 'image<float>::~image()'
../src/segment/segment-image.cpp:120: error: undefined reference to 'image<rgb>::image(int, int, bool)'
../src/segment/segment-image.cpp:121: error: undefined reference to 'image<int>::image(int, int, bool)'
../src/segment/pnmfile.h:166: error: undefined reference to 'image<rgb>::image(int, int, bool)'
../src/segment/pnmfile.h:166: error: undefined reference to 'image<rgb>::image(int, int, bool)'
collect2: ld returned 1 exit status
make: *** [HelloOpenCV2] Error 1
18:54:30 Build Finished (took 8s.826ms)
I can also confirm that .o (object) files for each of the source files are generated. However, I do not understand how to fix the undefined reference error.
Would like to add that the file image.cpp / image.h contain a class definition. I have not added these 2 files as add->class have added them as add->source and add->header
Just applied "nm" command on image.o. Got an empty response. Although image.o is a 24 byte file.
image.h contains the template class declaration and image.cpp contains class function definition.
Image is a template class. The header and source for it are as -
/*
* image.h
*
* Created on: 25 Oct, 2014
* Author: eeuser
*/
#ifndef IMAGE_H_
#define IMAGE_H_
#include <cstring>
/* use imRef to access image data. */
#define imRef(im, x, y) (im->access[y][x])
/* use imPtr to get pointer to image data. */
#define imPtr(im, x, y) &(im->access[y][x])
template <class T>
class image {
public:
/* create an image */
image(int width, int height, bool init = true);
/* delete an image */
~image();
/* init an image */
void init(const T &val);
/* copy an image */
image<T> *copy() const;
/* get the width of an image. */
int width() const { return w; }
/* get the height of an image. */
int height() const { return h; }
/* image data. */
T *data;
/* row pointers. */
T **access;
private:
int w, h;
};
#endif /* IMAGE_H_ */
The source:
/*
* image.cpp
*
* Created on: 25 Oct, 2014
* Author: eeuser
*/
#include "image.h"
template <class T>
image<T>::image(int width, int height, bool init) {
w = width;
h = height;
data = new T[w * h]; // allocate space for image data
access = new T*[h]; // allocate space for row pointers
// initialize row pointers
for (int i = 0; i < h; i++)
access[i] = data + (i * w);
if (init)
memset(data, 0, w * h * sizeof(T));
}
template <class T>
image<T>::~image() {
delete [] data;
delete [] access;
}
template <class T>
void image<T>::init(const T &val) {
T *ptr = imPtr(this, 0, 0);
T *end = imPtr(this, w-1, h-1);
while (ptr <= end)
*ptr++ = val;
}
template <class T>
image<T> *image<T>::copy() const {
image<T> *im = new image<T>(w, h, false);
memcpy(im->data, data, w * h * sizeof(T));
return im;
}
I turned on the extra warning flags in g++ (-Wextra). It complained about a bunch of static functions I had. I just changed those to normal functions. I guess this is ok, since those functions were not associated with a class.