Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

问题:编译动态库时,如何方便地链接静态库和动态库 #1110

Closed
duohappy opened this issue Dec 4, 2020 · 2 comments
Closed

Comments

@duohappy
Copy link
Contributor

duohappy commented Dec 4, 2020

注:提问题时若使用不能用/没效果/有问题/报错此类模糊表达,但又没有根据下面的模板给出任何相关辅助信息的,将会直接标记为Invalid。

描述问题

链接静态boost库,静态python库(安装完毕numpy库),静态opencv库,编译成一个动态库。

  • tree
.
├── build
│   └── linux
│       └── x86_64
│           └── release
│               └── libbuildBoostPython.so
├── src
│   ├── interface.cpp
│   ├── interface.h
│   └── main.cpp
└── xmake.lua
  • src

    • interface.h
#define BOOST_PYTHON_STATIC_LIB

#include <boost/python.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

#ifdef __cplusplus
extern "C" {
#endif

#if defined(_WIN32)
#   define __export         __declspec(dllexport)
#elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
#   define __export         __attribute__((visibility("default")))
#else
#   define __export
#endif

namespace py = boost::python;

__export py::list convertVectorToPyList();

#ifdef __cplusplus
}
#endif

++ interface.cpp

#include "interface.h"

py::list convertVectorToPyList()
{
    std::vector<int> vec { 2, 6, 9, 100};

    boost::python::list list;

    for (auto item : vec)
    {
        list.append(item);
    }

    return list;
}


BOOST_PYTHON_MODULE(hello_ext)
{
    py::def("convertVectorToPyList", convertVectorToPyList);
}

++ main.cpp

#include "interface.h"
#include <iostream>


int main(int argc, char** argv)
{
    py::list pylist = convertVectorToPyList();
    std::cout << "pylist: \n" << py::extract<char const*>(py::str(pylist)) << std::endl;
    return 0;
}
  • xmake.lua
add_rules("mode.debug", "mode.release")

target("buildBoostPython")
    set_kind("shared")
    add_includedirs("/home/duohappy/comSoftware/python-3.7.9-static/include/python3.7m", {public = true})
    add_linkdirs("/home/duohappy/comSoftware/python-3.7.9-static/lib")
    add_links("python3.7m")

    add_includedirs("/home/duohappy/comSoftware/opencv-3.4.10-static/include")
    add_linkdirs("/home/duohappy/comSoftware/opencv-3.4.10-static/lib")
    add_links("opencv_dnn", "opencv_highgui", "opencv_ml", "opencv_objdetect", "opencv_shape", "opencv_stitching", "opencv_superres", "opencv_videostab", "opencv_calib3d", "opencv_videoio", "opencv_imgcodecs", "opencv_features2d", "opencv_video", "opencv_photo", "opencv_imgproc", "opencv_flann", "opencv_core")
    
    add_includedirs("/home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74", {public = true})
    add_linkdirs("/home/duohappy/comSoftware/boost-1.7.4-static/lib")
    add_links("boost_python37-gcc7-mt-x64-1_74", "boost_numpy37-gcc7-mt-x64-1_74")

    add_links("z", "dl", "m", "pthread", "rt")
    
    add_files("src/interface.cpp")

target("buildBoostPython_demo")
    set_kind("binary")
    add_deps("buildBoostPython")
    add_files("src/main.cpp")
  • 手动链接
g++ interface.cpp -I/home/duohappy/srcCode/buildBoostPython -I/home/duohappy/comSoftware/python-3.7.9-static/include/python3.7m -I/home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74  -I /home/duohappy/comSoftware/opencv-3.4.10-static/include -Wl,-Bstatic -L/home/duohappy/comSoftware/boost-1.7.4-static/lib -lboost_python37-gcc7-mt-x64-1_74 -lboost_numpy37-gcc7-mt-x64-1_74 -L/home/duohappy/comSoftware/opencv-3.4.10-static/lib -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -L/home/duohappy/comSoftware/opencv-3.4.10-static/share/OpenCV/3rdparty/lib -littnotify -llibprotobuf -llibjpeg-turbo -llibwebp -llibpng -llibtiff -llibjasper -lIlmImf -lquirc -lippiw -lippicv -L/home/duohappy/comSoftware/python-3.7.9-static/lib -lpython3.7m -fPIC -shared -o hello_ext.so -Wl,-Bdynamic -L/usr/lib/x86_64-linux-gnu -lz -ldl -lm -lpthread -lrt
  • xmake信息
~/srcCode/buildBoostPython$ xmake -rv
[ 50%]: compiling.release src/interface.cpp
/usr/bin/gcc -c -m64 -fPIC -O3 -I/home/duohappy/comSoftware/python-3.7.9-static/include/python3.7m -I/home/duohappy/comSoftware/opencv-3.4.10-static/include -I/home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74 -o build/.objs/buildBoostPython/linux/x86_64/release/src/interface.cpp.o src/interface.cpp
[ 37%]: compiling.release src/main.cpp
/usr/bin/gcc -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -I/home/duohappy/comSoftware/python-3.7.9-static/include/python3.7m -I/home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74 -o build/.objs/buildBoostPython_demo/linux/x86_64/release/src/main.cpp.o src/main.cpp
[ 62%]: linking.release libbuildBoostPython.so
/usr/bin/g++ -o build/linux/x86_64/release/libbuildBoostPython.so build/.objs/buildBoostPython/linux/x86_64/release/src/interface.cpp.o -shared -fPIC -m64 -L/home/duohappy/comSoftware/python-3.7.9-static/lib -L/home/duohappy/comSoftware/opencv-3.4.10-static/lib -L/home/duohappy/comSoftware/boost-1.7.4-static/lib -s -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -lboost_python37-gcc7-mt-x64-1_74 -lboost_numpy37-gcc7-mt-x64-1_74 -lz -ldl -lm -lpthread -lrt -lpython3.7m
[ 87%]: linking.release buildBoostPython_demo
/usr/bin/g++ -o build/linux/x86_64/release/buildBoostPython_demo build/.objs/buildBoostPython_demo/linux/x86_64/release/src/main.cpp.o -m64 -L/home/duohappy/comSoftware/python-3.7.9-static/lib -L/home/duohappy/comSoftware/opencv-3.4.10-static/lib -L/home/duohappy/comSoftware/boost-1.7.4-static/lib -Lbuild/linux/x86_64/release -Wl,-rpath=$ORIGIN -s -lbuildBoostPython -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -lboost_python37-gcc7-mt-x64-1_74 -lboost_numpy37-gcc7-mt-x64-1_74 -lz -ldl -lm -lpthread -lrt -lpython3.7m
error: build/linux/x86_64/release/libbuildBoostPython.so: undefined reference to `forkpty'
build/linux/x86_64/release/libbuildBoostPython.so: undefined reference to `openpty'

期待的结果

达到手动编译的效果

错误信息

xmake -rvD,运行结果

[ 37%]: compiling.release src/interface.cpp
/usr/bin/gcc -c -m64 -fPIC -O3 -I/home/duohappy/comSoftware/python-3.7.9-static/include/python3.7m -I/home/duohappy/comSoftware/opencv-3.4.10-static/include -I/home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74 -o build/.objs/buildBoostPython/linux/x86_64/release/src/interface.cpp.o src/interface.cpp
[ 50%]: compiling.release src/main.cpp
/usr/bin/gcc -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -I/home/duohappy/comSoftware/python-3.7.9-static/include/python3.7m -I/home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74 -o build/.objs/buildBoostPython_demo/linux/x86_64/release/src/main.cpp.o src/main.cpp
In file included from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/detail/is_xxx.hpp:8:0,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/detail/is_auto_ptr.hpp:9,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/detail/copy_ctor_mutates_rhs.hpp:8,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/detail/value_arg.hpp:7,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/object/forward.hpp:10,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/object/pointer_holder.hpp:16,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/to_python_indirect.hpp:10,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/converter/arg_to_python.hpp:10,
In file included from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/detail/is_xxx.hpp:8:0,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/detail/is_auto_ptr.hpp:9,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/detail/copy_ctor_mutates_rhs.hpp:8,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/detail/value_arg.hpp:7,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/object/forward.hpp:10,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/object/pointer_holder.hpp:16,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/to_python_indirect.hpp:10,
                 from /home/duohappy/comSoftware/boost-1.7.4-static/include/boost-1_74/boost/python/converter/arg_to_python.hpp:10,
[ 62%]: linking.release libbuildBoostPython.so
/usr/bin/g++ -o build/linux/x86_64/release/libbuildBoostPython.so build/.objs/buildBoostPython/linux/x86_64/release/src/interface.cpp.o -shared -fPIC -m64 -L/home/duohappy/comSoftware/python-3.7.9-static/lib -L/home/duohappy/comSoftware/opencv-3.4.10-static/lib -L/home/duohappy/comSoftware/boost-1.7.4-static/lib -s -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -lboost_python37-gcc7-mt-x64-1_74 -lboost_numpy37-gcc7-mt-x64-1_74 -lz -ldl -lm -lpthread -lrt -lpython3.7m
[ 87%]: linking.release buildBoostPython_demo
/usr/bin/g++ -o build/linux/x86_64/release/buildBoostPython_demo build/.objs/buildBoostPython_demo/linux/x86_64/release/src/main.cpp.o -m64 -L/home/duohappy/comSoftware/python-3.7.9-static/lib -L/home/duohappy/comSoftware/opencv-3.4.10-static/lib -L/home/duohappy/comSoftware/boost-1.7.4-static/lib -Lbuild/linux/x86_64/release -Wl,-rpath=$ORIGIN -s -lbuildBoostPython -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -lboost_python37-gcc7-mt-x64-1_74 -lboost_numpy37-gcc7-mt-x64-1_74 -lz -ldl -lm -lpthread -lrt -lpython3.7m
error: @programdir/modules/private/async/runjobs.lua:210: @programdir/actions/build/kinds/binary.lua:73: @programdir/core/sandbox/modules/os.lua:258: build/linux/x86_64/release/libbuildBoostPython.so: undefined reference to `forkpty'
build/linux/x86_64/release/libbuildBoostPython.so: undefined reference to `openpty'
collect2: error: ld returned 1 exit status

stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:789]: in function 'raise'
    [@programdir/core/sandbox/modules/os.lua:258]: in function 'runv'
    [@programdir/modules/core/tools/gcc.lua:356]:
    [C]: in function 'link'
    [@programdir/actions/build/kinds/binary.lua:73]: in function 'callback'
    [@programdir/modules/core/project/depend.lua:186]: in function 'on_changed'
    [@programdir/actions/build/kinds/binary.lua:54]: in function '_do_link_target'
    [@programdir/actions/build/kinds/binary.lua:94]:
    [@programdir/actions/build/kinds/binary.lua:115]: in function '_link_target'
    [@programdir/actions/build/kinds/binary.lua:137]: in function 'jobfunc'
    [@programdir/modules/private/async/runjobs.lua:188]:
    [C]: in function 'trycall'
    [@programdir/core/sandbox/modules/try.lua:121]: in function 'try'
    [@programdir/modules/private/async/runjobs.lua:182]: in function 'cotask'
    [@programdir/core/base/scheduler.lua:317]:

stack traceback:
	[C]: in function 'error'
	@programdir/core/base/os.lua:789: in function 'raise'
	@programdir/modules/private/async/runjobs.lua:210: in function 'catch'
	@programdir/core/sandbox/modules/try.lua:127: in function 'try'
	@programdir/modules/private/async/runjobs.lua:182: in function 'cotask'
	@programdir/core/base/scheduler.lua:317: in function <@programdir/core/base/scheduler.lua:315>

相关环境

  • xmake版本:2.3.9
  • xmake运行平台:ubuntu18.04
  • xmake目标平台:

其他信息

混合链接静态库和动态库,会出现这样的问题,如果单纯全部用动态库不会出现。

-Wl,-Bstatic会导致后面的默认全部链接静态库
在手动编译时,最后需要加上 -Wl,-Bdynamic来链接如pthread之类的库,这类的库也有静态库,但是如果全部默认采用静态库时,没有用-Wl,-Bdynamic,会产生 cannot find -lgcc_s错误

@duohappy duohappy changed the title 链接静态库和动态库,编译动态库 问题:编译动态库时,如何方便地链接静态库和动态库 Dec 4, 2020
@waruqi
Copy link
Member

waruqi commented Dec 4, 2020

可以调整下链接顺序 使得跟你手动链接的顺序一致 可以用 add_syslinks 将一些 z dl pthread系统库置后

另外排查下缺少的openpty符号来自哪个库 你的哪些links依赖使用了它,然后针对性调整链接顺序

可以用 nm -D xx.so
nm xxx.a | grep openpty .看下是哪个库里使用和定义的

@FlowerFerry
Copy link

像这样可不可以?
add_ldflags("-Wl,-Bstatic -lboost_python37-gcc7-mt-x64-1_74 -Wl,-Bdynamic", {force = true})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants