C++共享库:创建和使用
C++ Shared library: Creation and usage
我正在尝试构建一个共享对象,以便以后使用其他项目中共享对象DoSomethingUseful()
的函数。 它使用外部库以及我在多个项目中使用的一堆标头。
使用 CMake,我创建了一个项目 MySharedLib,其中包含一个名为library.h
:
#ifndef MYSHAREDLIB_LIBRARY_H
#define MYSHAREDLIB_LIBRARY_H
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstdio>
// own header files
#include <header1.h>
#include <header2.h>
#define PI 3.14159265
//tesseract
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
//openCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//face detection
#include "opencv2/objdetect.hpp"
#include "opencv2/imgproc.hpp"
void DoSomethingUseul(int[] inparray);
#endif
library.cpp
为
#include "library.h"
void DoSomethingUseful(int[] inparray){...}
我的CMake文件是这样的:
cmake_minimum_required(VERSION 3.10)
project(MYSHAREDLIB)
find_package( OpenCV REQUIRED )
set(CMAKE_CXX_STANDARD 11)
set(MY_INCLUDE_DIR ../source/)
set(MY_OPENCV_CASCADES /opencvpath/openCV34/opencv/sources/data/haarcascades/)
include_directories(${MY_INCLUDE_DIR} ${MY_OPENCV_CASCADES} /usr/include)
link_directories(${MY_INCLUDE_DIR})
add_library(MYSHAREDLIB SHARED library.cpp library.h
${MY_INCLUDE_DIR}header1.cpp
${MY_INCLUDE_DIR}header1.h
${MY_INCLUDE_DIR}header2.cpp
${MY_INCLUDE_DIR}header2.h
)
set_target_properties(MYSHAREDLIB PROPERTIES VERSION 3.10)
set_target_properties(MYSHAREDLIB PROPERTIES SOVERSION 1)
target_link_libraries(MYSHAREDLIB lept tesseract ${OpenCV_LIBS})
*.so 文件是成功创建的,即使用 Clion,不会抛出任何错误,并且文件libMySharedLib.so
在那里。
但是,当我想在另一个文件中使用该函数DoSomethingUseful()
时DoSomething.cpp
:
#include <iostream>
#include "library.h"
using namespace std;
int main()
{
int[2] myarray; myarray[0]=1; myarray[1] =2;
DoSomethingUseful(myarray);
return 0;
}
和
g++ -g -Wall -o DoSomething DoSomething.cpp -I ../source/ -L. libMYSHAREDLIB.so
当我执行时
./DoSomething
我得到:
./DoSomething: error while loading shared libraries: libMYSHAREDLIB.so: cannot open shared object file: No such file or directory
之前,我在编译时没有-I ../source/
产生:
In file included from DoSomething.cpp:8:0:
library.h:19:10: fatal error: header1.h: No such file or directory
#include <header1.h>
我发现许多线程通常都在处理此问题,并且我已经从这些问题中收集了很多有关共享对象的知识,并且还成功地从运行的各种教程中获取了示例。 但是,我没有成功开展自己的项目。
这只是众多问题之一,但我希望我能在这里得到帮助,也许还有一般提示。非常感谢您的任何帮助。
假设Linux(如果错误,请修改问题(。运行可执行文件时,共享库仅从/etc/ld.so.conf 中列出的路径加载。如果你想从其他地方加载(.(,你必须设置LD_LIBRARY_PATH环境变量,例如LD_LIBRARY_PATH=。 ./DoSomething
相关文章:
- 在为LINUX创建共享库时,如何避免STL的私有/弱副本
- 在创建共享库时,是否可以明确地排除某些类
- 为什么从引用创建共享指针会复制对象?
- Android :需要在 c++ NDK 中创建共享首选项对象并存储一些布尔值
- 使用多个标头使用 CMake 创建C++共享库
- 如何从静态c和c++库创建共享c库
- G 在创建共享对象时无法链接库
- 在创建共享对象时,不能使用针对未定义符号的重定位R_X86_64_PC32;使用-fPIC重新编译
- C++:从静态库创建共享对象
- 当我可以创建共享指针时,为什么我不能创建指向数组的唯一指针?
- 我可以在libleveldb.a中找到符号,但是当我创建共享库时,找不到相同的符号。
- 如何在HP-UX中使用GCC创建共享库
- 创建共享_ptr似乎会导致segfault
- 有没有办法使用 WINAPI 或 Embarcadero C++ Builder XE 创建共享目录/文件夹
- 使用openmp创建共享锁
- 如何使用 ld 创建共享库
- 如何在 OSX 上使用 glfw3 在 OpenGL 和 OpenCL 之间创建共享上下文
- 如何在linux x64上创建共享库对象,该对象内部使用C++异常,并且可以在旧平台上运行
- 是否可以使用 std::shared_ptr 创建共享对象池,并在没有自定义析构函数的情况下创建weak_ptr
- 如何在 C++ 中为安卓和 iOS 创建共享库