Linking Error of FBX SDK on CMake - linux

I encountered an error when I am trying to link the Fbx static library (libfbxsdk.a) to a project on Linux using CMake that keeps reporting for undefined references to fbx-related functions. I checked the configuration in the CMakelist and everything looks alright, but the program cannot be compiled statically or dynamically. I really don't have any idea why the static library is not linked properly and appreciate any helps to fix the linking issue. Here is my cmakelist file for static linking:
cmake_minimum_required(VERSION 3.6)
SET(CMAKE_VERBOSE_MAKEFILE ON)
project(FBX_SDK)
add_definitions(-DFBXSDK_NEW_API)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lm -lrt -luuid -lstdc++ -lpthread -ldl -lX11 -std=c++11")
include_directories(${X11_LIBRARIES}
${FBX_SDK_SOURCE_DIR}/fbxsdk-1.2/include/)
link_directories(${X11_LIBRARIES}
${FBX_SDK_SOURCE_DIR}/fbxsdk-1.2/Lib/Linux/Debug_x64/)
set(SOURCE_FILES main.cpp)
add_executable(FBX_SDK ${SOURCE_FILES})
target_link_libraries(FBX_SDK
${FBX_SDK_SOURCE_DIR}/fbxsdk-1.2/Lib/Linux/Debug_x64/libfbxsdk.a)
and my cpp file is as below:
#include <iostream>
#include <fbxsdk.h>
void InitializeSDKObjects(FbxManager* &pManager, FbxScene* &pScene);
int main()
{
std::cout << "Hello, World!" << std::endl;
FbxManager* lSdkManager = NULL;
FbxScene* lScene = NULL;
bool lResult;
InitializeSDKObjects(lSdkManager, lScene);
return 0;
}
void InitializeSDKObjects(FbxManager* &pManager, FbxScene* &pScene)
{
pManager = FbxManager::Create();
if(!pManager)
{
FBXSDK_printf("Error: Unable to create FBX Manager!\n");
exit(1);
}
else FBXSDK_printf("Autodesk FBX SDK version %s\n", pManager->GetVersion());
FbxIOSettings* ios = FbxIOSettings::Create(pManager, IOSROOT);
pManager->SetIOSettings(ios);
FbxString lPath = FbxGetApplicationDirectory();
pManager->LoadPluginsDirectory(lPath.Buffer());
pScene = FbxScene::Create(pManager, "My Scene");
if(!pScene)
{
FBXSDK_printf("Error: Unable to create FBX scene!\n");
exit(1);
}
}
While the include and lib folders of fbx sdk are like this:

Related

How to link properly Dcmtk with Qt for Linux?

My goal is open Dicom files and convert thes into cv::Mat to process them with Opencv.
I have compiled dcmtk 3.6.3 on ubuntu 18.4.1 and tried to link it with Qt 5.11.1 with Qt Creator 4.6.2 but failed to do so.
# pro file
QT += core
QT -= gui
TARGET = DcmtkTesting
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
DCMTK_PREFIX = "/home/ismail/dcmtk363"
DCMTK_LIBS_PREFIX=$$DCMTK_PREFIX"/lib"
DCMTK_INCLUDE=$$DCMTK_PREFIX"/include"
INCLUDEPATH+=$$DCMTK_INCLUDE
LIBS += -L$$DCMTK_LIBS_PREFIX
SOURCES += main.cpp
and for the main:
#include
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include <dcmtk/dcmimgle/dcmimage.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
DicomImage *image = new DicomImage("test.dcm");
if (image != NULL)
{
if (image->getStatus() == EIS_Normal)
{
if (image->isMonochrome())
{
image->setMinMaxWindow();
Uint8 *pixelData = (Uint8 *)(image->getOutputData(8 /* bits*/));
if (pixelData != NULL)
{
/* do something useful with the pixel data */
}
}
} else
cout << "Error: cannot load DICOM image (" << DicomImage::getString(image->getStatus()) << ")" << endl;
}
delete image;
return a.exec();
}
and I got this errors:
The error indicates that the linker could not find the symbols (methods) provided by the library. In your .pro file, you pointed the linker to a directory where your library is located, but you forgot to specify which library should be linked.
So you have to modify the line LIBS +=... accordingly, e.g.:
LIBS += -L$$DCMTK_LIBS_PREFIX -ldcmtk
Since I don't know the actual name of the library, I use dcmtk in my example. You may have to adopt it to fit your build environment. Just make sure that you have the -l (lower case L), immediately followed by the library name.

Pass preprocessor definition to clang++

Given
void foo() {
int i = 0;
#ifdef MACRO_A
// custom behaviour
#endif
// program code
}
Is it possible to pass #define MACRO_A to clang++ when compiling to allow the 'custom behavour' statements to come into effect? I cannot find documentation which suggests this is possible in clang++, but it IS possible in other compilers (g++).
The command, thanks to pevasquez's help, is
clang++ -D MACRO_A main.cpp -o main
The function
void foo() {
int i = 0;
#ifdef MACRO_A
// custom behaviour
#endif
// program code
}
will compile with the code in the pre-processor directive section being included.
This will be an addition to my makefiles to create a debug friendly make-chain as well and the production version.

Libicu, undefined reference while compiling

I'm trying to use the lib icu in my shared library.
So i generate my .so who use libicu functions, this parts works well.
Then i try to compile my main program, using the shared library that i created before. But i get the following error:
release//libstring.so: undefined reference to `u_errorName_53'
release//libstring.so: undefined reference to `ucnv_toUnicode_53'
release//libstring.so: undefined reference to `ucnv_close_53'
release//libstring.so: undefined reference to `ucnv_fromUChars_53'
release//libstring.so: undefined reference to `udat_open_53'
release//libstring.so: undefined reference to `udat_parse_53'
release//libstring.so: undefined reference to `udat_format_53'
release//libstring.so: undefined reference to `ucnv_toUChars_53'
release//libstring.so: undefined reference to `ucnv_open_53'
release//libstring.so: undefined reference to `udat_close_53'
I have installed the libicu with this help.
I compile my shared library with -licuuc, as well as my main program compile only with -l:libstring.so and -licuuc.
When i do ldd libstring.so, i don't have the .so of the libicu on the list of dependencies.
The problem probably come from this.
Thx
EDIT: I have create a little test:
icu.h:
#pragma once
#include <iostream>
#include <string>
#include <unicode/ucnv.h>
class c_icu
{
private:
void * p_priv;
public:
c_icu();
void open(const wchar_t *name);
void to_local(std::wstring &, std::wstring &);
void from_local(std::wstring &, std::wstring &);
void close(void);
~c_icu();
};
icu.cpp:
#include "icu.h"
c_icu::c_icu()
{
p_priv = NULL;
}
c_icu::~c_icu()
{
if (p_priv)
close();
}
void c_icu::open(const wchar_t *name)
{
UErrorCode status = U_ZERO_ERROR;
char conv_name[256];
if (wcstombs(conv_name, name, 256) == (size_t)-1)
{
std::cout << "ERROR: wcstombs failed" << std::endl;
exit(0);
}
if ((p_priv = (UConverter *)ucnv_open(conv_name, &status)) == NULL)
{
std::cout << "ERROR: ucnv_open failed" << std::endl;
exit(0);
}
}
void c_icu::to_local(std::wstring &src, std::wstring &dst)
{
UErrorCode status = U_ZERO_ERROR;
int32_t len;
len = ucnv_toUChars((UConverter *)p_priv, (UChar *)NULL, 0, (const char *)src.c_str(), src.size(), &status);
if (status == U_BUFFER_OVERFLOW_ERROR)
{
status = U_ZERO_ERROR;
dst.resize(len);
len = ucnv_toUChars((UConverter *)p_priv, (UChar *)dst.c_str(), dst.size(), (const char *)src.c_str(), src.size(), &status);
if (U_FAILURE(status))
{
std::cout << "ERROR: to_local failed" << std::endl;
exit(0);
}
}
}
void c_icu::from_local(std::wstring &src, std::wstring &dst)
{
UErrorCode status = U_ZERO_ERROR;
int32_t len;
len = ucnv_fromUChars((UConverter *)p_priv, (char *)NULL, 0, (const UChar *)src.c_str(), src.size(), &status);
if (status == U_BUFFER_OVERFLOW_ERROR)
{
status = U_ZERO_ERROR;
dst.reserve(len);
len = ucnv_fromUChars((UConverter *)p_priv, (char *)dst.c_str(), dst.size(), (const UChar *)src.c_str(), -1, &status);
}
}
void c_icu::close(void)
{
ucnv_close((UConverter *)p_priv);
p_priv = NULL;
}
test.cpp:
#include "icu.h"
int main(void)
{
c_icu converter;
std::wstring src = L"";
std::wstring dst;
const wchar_t add[11] = L"Just a test";
const wchar_t *unicode = L"koi8-r";
for (unsigned int i = 0; i < 1000; i++)
src.append(add, 11);
converter.open(unicode);
converter.from_local(src, dst);
converter.close();
}
Makefile:
NAME= test
LIB_NAME= libicutest.so
FLAGS= -W -Wall -Wextra
all: lib $(NAME)
$(NAME):
#echo "Main programm compiling ..."
g++ -c test.cpp -o test.o $(FLAGS)
g++ -o $(NAME) test.o -Wl,-rpath,'$$ORIGIN/' -licui18n -L. -licutest
#echo "Main programm compiled"
lib:
#echo "Lib Compiling ..."
g++ -std=gnu++11 -c -fPIC icu.cpp -o icu.o $(FLAGS)
g++ -shared -Wl,-soname,$(LIB_NAME) -o $(LIB_NAME) icu.o -licui18n
#echo "Lib Compiled"
Note: even with -licuuc instead of -licui18n i got the same error.
Output:
$> make
Lib Compiling ...
g++ -std=gnu++11 -c -fPIC icu.cpp -o icu.o -W -Wall -Wextra
g++ -shared -Wl,-soname,libicutest.so -o libicutest.so icu.o -licui18n
Lib Compiled
Main programm compiling ...
g++ -c test.cpp -o test.o -W -Wall -Wextra
g++ -o test test.o -Wl,-rpath,'$ORIGIN/' -licui18n -L. -licutest
./libicutest.so: undefined reference to `ucnv_close_53'
./libicutest.so: undefined reference to `ucnv_fromUChars_53'
./libicutest.so: undefined reference to `ucnv_toUChars_53'
./libicutest.so: undefined reference to `ucnv_open_53'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
If i do ldd on my shared library:
$> ldd libicutest.so
linux-vdso.so.1 => (0x00007ffd5eb7f000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f092b5e6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f092b221000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f092af1a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f092bb00000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f092ad04000)
EDIT2:
Ok, seems that the problem is easier to solve than expected. My program need function in icu_53, but i got icu_48 installed on my computer dunno why.
$> locate libicu
/usr/lib/x86_64-linux-gnu/libicudata.so.48
/usr/lib/x86_64-linux-gnu/libicudata.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicui18n.so.48
/usr/lib/x86_64-linux-gnu/libicui18n.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicuio.so.48
/usr/lib/x86_64-linux-gnu/libicuio.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicule.so.48
/usr/lib/x86_64-linux-gnu/libicule.so.48.1.1
/usr/lib/x86_64-linux-gnu/libiculx.so.48
/usr/lib/x86_64-linux-gnu/libiculx.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicutest.so.48
/usr/lib/x86_64-linux-gnu/libicutest.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicutu.so.48
/usr/lib/x86_64-linux-gnu/libicutu.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicuuc.so.48
/usr/lib/x86_64-linux-gnu/libicuuc.so.48.1.1
Does anyone know how to upgrade the version from icu_48 to icu_53 at least ?
Seems 'udat_parse_53' is located inside libicui18n.so.53 library. Try to compile your project with '-licui18n' option.

No Rule to Make Target - New in Android NDK

I am making an attempt to write an NDK basic app to understand how NDK works. I have a text view and a button in the MainActivity and a library class HelloWorldLib.java which have the static native function helloWorld. I have created the header file copied it and created the ".c" file in the jni folder.
When i am building via ndk-build i am getting the error "no rule to make target" error. I checked lots of posts and answer but nothing worked.
I included a test.c empty file as indicated in the below link and was able to build the project,However, when i ran my app i got the error that " no native implementation found" which is bizzare because i have the implementation.
https://code.google.com/p/android/issues/detail?id=66937
OnClick from where HelloWorldLib is called.
public void onClick(View v) {
// TODO Auto-generated method stub
String inNDK = HelloWorldLib.helloWorld();
tv.setText(inNDK);
}
HelloWorldLib where native is funciton is defined.
public class HelloWorldLib {
public native static String helloWorld();
static{
System.loadLibrary("com.example.androidndk_HelloWorldLib");
}
}
header file created by Javah -jni
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_androidndk_HelloWorldLib */
#ifndef _Included_com_example_androidndk_HelloWorldLib
#define _Included_com_example_androidndk_HelloWorldLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_androidndk_HelloWorldLib
* Method: helloWorld
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_androidndk_HelloWorldLib_helloWorld
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
C file copied from .h and then modified.
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <com_example_androidndk_HelloWorldLib.h>
#include <string.h>
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_androidndk_HelloWorldLib_helloWorld
(JNIEnv *env, jclass clazz){
return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI");
}
Andoid.mk file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := com.example.androidndk_HelloWorldLib.c
LOCAL_MODULE := com.example.androidndk_HelloWorldLib
include $(BUILD_SHARED_LIBRARY)
The error i am getting in the command prompt is shown below:
D:\Users\gabhatia\Desktop\Android SDK\MyWorkspace\AndroidNDK>ndk-build
make.exe: *** No rule to make target `jni/com.example.androidndk_HelloWorldLib.c
', needed by `obj/local/x86/objs/com.example.androidndk_HelloWorldLib/com.exampl
e.androidndk_HelloWorldLib.o'. Stop.
I am not sure where i am getting wrong but any help will be greatly appreciated.
Thanks.
GB.
Here
LOCAL_SRC_FILES := com.example.androidndk_HelloWorldLib.c
LOCAL_MODULE := com.example.androidndk_HelloWorldLib
change to
FILE_LIST := $(wildcard $(LOCAL_PATH)/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
Have a try.

Dynamically loading Linux shared libraries?

I want to create a shared library which can be loaded in two different ways into targets:
LD_PRELOAD
Dynamic loading via dlsym
My shared library looks like this:
#include "stdio.h"
void __attribute__ ((constructor)) my_load(void);
void my_load(void) {
printf("asdf");
}
void someFunc(void) {
printf("someFunc called");
}
I am compiling it like so:
all:
gcc -fPIC -g -c -Wall MyLib.c
gcc -shared -W1,-soname,MyLib.so.1 -o MyLib.so.1.0.1 -lc
I do not wish to install it with ldconfig, etc. The target process looks like this:
#include <stdio.h>
#include <dlfcn.h>
void func1() {
printf("%d\n", 1);
}
void func2() {
printf("%d\n", 2);
}
void func3() {
printf("%d\n", 3);
}
int main() {
void* lib_handle = dlopen("/home/mike/Desktop/TargetProcess/MyLib.so.1.0.1",
RTLD_NOW|RTLD_GLOBAL);
if(lib_handle == NULL) {
printf("Failed loading lib\n");
} else {
printf("Loaded lib successfully\n");
void (*some_func)() = dlsym(lib_handle, "someFunc");
printf("%p\n", some_func);
dlclose(lib_handle);
}
func1();
func2();
func3();
return 0;
}
The target is compiled as so:
all:
gcc TestProg.c -ldl -o TestProg
My questions are:
With the dynamic loading with dlopen as above, why does my_load not appear to be called?
With the same method, why does dlsym always return nil even though dlopen returns non-null? Similarly, nm doesn't list either my_load or someFunc as symbols of the .so.
Is it possible to use LD_PRELOAD to load the library? I tried copying the .so into the same directory as the target then invoking LD_PRELOAD="./MyLib.so.1.0.1" ./TestProg but again my_load seems not to be being called.
Your object files was no linked into your library:
gcc -shared -W1,-soname,MyLib.so.1 -o MyLib.so.1.0.1 -lc
Change it to include your object file MyLib.o:
gcc MyLib.o -shared -W1,-soname,MyLib.so.1 -o MyLib.so.1.0.1 -lc
UPDATE: just tryed your command locally (without any MyLib.c or MyLib.o):
$ gcc -shared -W1,-soname,MyLib.so.1 -o MyLib.so.1.0.1 -lc && echo ok
ok
$ nm MyLib.so.1.0.1
xxxxxxxx a _DYNAMIC
xxxxxxxx a _GLOBAL_OFFSET_TABLE_
w _Jv_RegisterClasses
xxxxxxxx A __bss_start
w __cxa_finalize##xxxxxxxxxxx
xxxxxxxx d __dso_handle
w __gmon_start__
xxxxxxxx t __i686.get_pc_thunk.bx
xxxxxxxx A _edata
xxxxxxxx A _end
xxxxxxxx T _fini
xxxxxxxx T _init
It is an empty dynamic library.

Resources