Closed14

Swift と C++ の混合プロジェクトで、OpenCV を C++ から使用したい【Swift 5.3.2 AArch64 Linux】

treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ 解決した問題

その1: OpenCV が見つからずコンパイルできない

swift run -Xcxx=-I/usr/local/include/opencv4 のように、OpenCV のパスを教えてあげる。
https://zenn.dev/treastrain/scraps/b3690cc6c7c473#comment-50d1678d43762c

その2: cv::Mat が見つからない

swift のリンカーオプションの指定方法が間違っていた。-Xlinker を用いて指定する。
https://zenn.dev/treastrain/scraps/b3690cc6c7c473#comment-88e6008cf64940

treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

ファイルツリーと Package.swift

Package.swift
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "OpenCVSwiftCXX",
    products: [
        .executable(name: "OpenCVSwiftCXX", targets: ["OpenCVSwiftCXX"]),
        .library(name: "CXXWrapper", targets: ["CXXWrapper"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "CXXWrapper",
            exclude: [
                "sources/cxx_lib.hpp",
                "include/cxx_bridge.hpp"
            ]),
        .target(
            name: "OpenCVSwiftCXX",
            dependencies: ["CXXWrapper"]),
    ],
    cLanguageStandard: .c11,
    cxxLanguageStandard: .cxx1z
)
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

ファイル生成手順

  1. $ swift --version
    Swift version 5.3.2 (swift-5.3.2-RELEASE)
    Target: aarch64-unknown-linux-gnu
  2. $ mkdir OpenCVSwiftCXX
  3. $ cd OpenCVSwiftCXX
  4. $ swift package init --type executable
  5. $ cd Sources
  6. $ mkdir CXXWrapper
  7. $ cd CXXWrapper
  8. $ touch cxx_bridge.cpp
  9. $ mkdir include
  10. $ touch include/cxx_bridge.hpp
  11. $ mkdir sources
  12. $ touch sources/cxx_lib.cpp
  13. $ touch sources/cxx_lib.hpp
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

C++ のコード CXXWrapper

Sources/CXXWrapper/include/cxx_bridge.hpp
#ifndef CXX_BRIDGE_HPP
#define CXX_BRIDGE_HPP

#ifdef __cplusplus
extern "C" {
#endif

void printOpenCVVersion();

#ifdef __cplusplus
}
#endif

#endif //CXX_BRIDGE_HPP
Sources/CXXWrapper/cxx_bridge.cpp
#include <cxx_bridge.hpp>

#include <string>
#include "sources/cxx_lib.hpp"

#ifdef __cplusplus
extern "C"
#endif

void printOpenCVVersion() {
    print_cv_version();
}
Sources/CXXWrapper/sources/cxx_lib.hpp
#ifndef CXX_LIB_HPP
#define CXX_LIB_HPP

#include <string>

void print_cv_version();
void make_mat();

#endif //CXX_LIB_HPP
Sources/CXXWrapper/sources/cxx_lib.cpp
#include "cxx_lib.hpp"
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>

void print_cv_version() {
    std::cout << "OpenCV version : " << CV_VERSION << std::endl;
    std::cout << "Major version : " << CV_MAJOR_VERSION << std::endl;
    std::cout << "Minor version : " << CV_MINOR_VERSION << std::endl;
    std::cout << "Subminor version : " << CV_SUBMINOR_VERSION << std::endl;

    // make_mat();
}

/*
void make_mat() {
    cv::Mat grayImage;
}
*/
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

Swift のコード OpenCVSwiftCXX

Sources/OpenCVSwiftCXX/main.swift
import Foundation
import CXXWrapper

func printDate() {
    let date = Date()
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "ja_JP")
    formatter.dateStyle = .full
    formatter.timeStyle = .full
    print("Date:", formatter.string(from: date))
}

printDate()

#if os(Linux)
system("uname -a")
#endif

printOpenCVVersion()
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ その1 - 問題: swift run

以上の Swift、C++ のコードを swift run すると、OpenCV が見つからない旨のエラーが出る。

$ swift run
: 'opencv2/opencv.hpp' file not found
#include <opencv2/opencv.hpp>
         ^~~~~~~~~~~~~~~~~~~~
1 error generated.
[1/4] Compiling cxx_lib.cpp
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ その1 - 調査: swift run

この OpenCV が見つからないエラーは、C++ のみのプロジェクトでも発生する。コンパイラにオプションで OpenCV のパスを渡すことで解消される。

main.cpp
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>

void print_cv_version() {
    std::cout << "OpenCV version : " << CV_VERSION << std::endl;
    std::cout << "Major version : " << CV_MAJOR_VERSION << std::endl;
    std::cout << "Minor version : " << CV_MINOR_VERSION << std::endl;
    std::cout << "Subminor version : " << CV_SUBMINOR_VERSION << std::endl;
}

int main() {
    print_cv_version();

    // cv::Mat hsvImage;

    return 0;
}
$ clang++ main.cpp 
main.cpp:5:10: fatal error: 'opencv2/opencv.hpp' file not found
#include <opencv2/opencv.hpp>
         ^~~~~~~~~~~~~~~~~~~~
1 error generated.
$ clang++ main.cpp -I/usr/local/include/opencv4
$ ./a.out
OpenCV version : 4.5.1
Major version : 4
Minor version : 5
Subminor version : 1
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ その1 - 解決: swift run

C++ コンパイラに OpenCV のパスを教えてあげる。

$ swift run -Xcxx=-I/usr/local/include/opencv4
[6/6] Linking OpenCVSwiftCXX
Date: 2021年1月10日日曜日 17時04分46秒 日本標準時
Linux ubuntu 5.4.0-1026-raspi #29-Ubuntu SMP PREEMPT Mon Dec 14 17:01:16 UTC 2020 aarch64 aarch64 aarch64 GNU/Linux
OpenCV version : 4.5.1
Major version : 4
Minor version : 5
Subminor version : 1
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ その2 - 問題: undefined reference to 'cv::Mat::Mat()'

Sources/CXXWrapper/sources/cxx_lib.cpp を次のように変更し(コメントを外す)、swift run -Xcxx=-I/usr/local/include/opencv4 すると、undefined reference to 'cv::Mat::Mat()' エラーとなる。

Sources/CXXWrapper/sources/cxx_lib.cpp
#include "cxx_lib.hpp"
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>

void print_cv_version() {
    std::cout << "OpenCV version : " << CV_VERSION << std::endl;
    std::cout << "Major version : " << CV_MAJOR_VERSION << std::endl;
    std::cout << "Minor version : " << CV_MINOR_VERSION << std::endl;
    std::cout << "Subminor version : " << CV_SUBMINOR_VERSION << std::endl;

    make_mat();
}

void make_mat() {
    cv::Mat grayImage;
}
$ swift run -Xcxx=-I/usr/local/include/opencv4
Sources/CXXWrapper/sources/cxx_lib.cpp:16: error: undefined reference to 'cv::Mat::Mat()'
Sources/CXXWrapper/sources/cxx_lib.cpp:17: error: undefined reference to 'cv::Mat::~Mat()'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
link command failed with exit code 1 (use -v to see invocation)
[5/6] Linking OpenCVSwiftCXX
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ その2 - 調査: undefined reference to 'cv::Mat::Mat()'

この cv::Mat が見つからないエラーは、C++ のみのプロジェクトでも発生する。コンパイラにオプション -lopencv_core を指定することで解消される。

main.cpp
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>

void print_cv_version() {
    std::cout << "OpenCV version : " << CV_VERSION << std::endl;
    std::cout << "Major version : " << CV_MAJOR_VERSION << std::endl;
    std::cout << "Minor version : " << CV_MINOR_VERSION << std::endl;
    std::cout << "Subminor version : " << CV_SUBMINOR_VERSION << std::endl;
}

int main() {
    print_cv_version();

    cv::Mat hsvImage;

    return 0;
}
$ clang++ main.cpp -I/usr/local/include/opencv4
/usr/bin/ld: /tmp/main-5bfd36.o: in function `main':
main.cpp:(.text+0xe0): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: main.cpp:(.text+0xec): undefined reference to `cv::Mat::~Mat()'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang++ main.cpp -I/usr/local/include/opencv4 -lopencv_core
$ ./a.out
OpenCV version : 4.5.1
Major version : 4
Minor version : 5
Subminor version : 1
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ その2 - 試行: undefined reference to 'cv::Mat::Mat()'

オプション -lopencv_core を指定するが、同様のエラーで失敗する。

$ swift run -Xcxx=-I/usr/local/include/opencv4
Sources/CXXWrapper/sources/cxx_lib.cpp:16: error: undefined reference to 'cv::Mat::Mat()'
Sources/CXXWrapper/sources/cxx_lib.cpp:17: error: undefined reference to 'cv::Mat::~Mat()'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
link command failed with exit code 1 (use -v to see invocation)
[5/6] Linking OpenCVSwiftCXX

$ swift run -Xcxx=-I/usr/local/include/opencv4 -Xcxx=-lopencv_core
-lopencv_core: 'linker' input unused [-Wunused-command-line-argument]
: -lopencv_core: 'linker' input unused [-Wunused-command-line-argument]
Sources/CXXWrapper/sources/cxx_lib.cpp:16: error: undefined reference to 'cv::Mat::Mat()'
Sources/CXXWrapper/sources/cxx_lib.cpp:17: error: undefined reference to 'cv::Mat::~Mat()'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
link command failed with exit code 1 (use -v to see invocation)
[3/4] Linking OpenCVSwiftCXX
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ その2 - 試行の詳細: undefined reference to 'cv::Mat::Mat()'

`swift run -Xcxx=-I/usr/local/include/opencv4 -Xcxx=-lopencv_core -Xcxx=-v -v` (`swift` と C++ コンパイラの両方に `--verbose` を指定)の実行結果。
$ swift run -Xcxx=-I/usr/local/include/opencv4 -Xcxx=-lopencv_core -Xcxx=-v -v
/home/ubuntu/Developer/Libraries/swift/usr/bin/swiftc -print-target-info
/home/ubuntu/Developer/Libraries/swift/usr/bin/swiftc -L /home/ubuntu/Developer/Libraries/swift/usr/lib/swift/pm/4_2 -lPackageDescription -Xlinker -rpath -Xlinker /home/ubuntu/Developer/Libraries/swift/usr/lib/swift/pm/4_2 -swift-version 5 -I /home/ubuntu/Developer/Libraries/swift/usr/lib/swift/pm/4_2 -package-description-version 5.3.0 /home/ubuntu/Developer/OpenCVSwiftCXX/Package.swift -o /tmp/TemporaryDirectory.qIMkeC/opencvswiftcxx-manifest
/tmp/TemporaryDirectory.qIMkeC/opencvswiftcxx-manifest -fileno 9
/home/ubuntu/Developer/Libraries/swift/usr/bin/clang -target aarch64-unknown-linux-gnu -fPIC -g -O0 -DSWIFT_PACKAGE=1 -DDEBUG=1 -fblocks -fmodules -fmodule-name=CXXWrapper -I /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include -fmodules-cache-path=/home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -I/usr/local/include/opencv4 -lopencv_core -v -MD -MT dependencies -MF /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/cxx_bridge.cpp.d -std=c++1z -c /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/cxx_bridge.cpp -o /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/cxx_bridge.cpp.o
/home/ubuntu/Developer/Libraries/swift/usr/bin/clang -target aarch64-unknown-linux-gnu -fPIC -g -O0 -DSWIFT_PACKAGE=1 -DDEBUG=1 -fblocks -fmodules -fmodule-name=CXXWrapper -I /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include -fmodules-cache-path=/home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -I/usr/local/include/opencv4 -lopencv_core -v -MD -MT dependencies -MF /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/sources/cxx_lib.cpp.d -std=c++1z -c /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/sources/cxx_lib.cpp -o /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/sources/cxx_lib.cpp.o
clang version 10.0.0 (https://github.com/apple/llvm-project.git 3093af41dd65ad466dcd5603e9289244edfee4f5)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/ubuntu/Developer/Libraries/swift/usr/bin
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/aarch64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
clang-10: warning: -lopencv_core: 'linker' input unused [-Wunused-command-line-argument]
 "/home/ubuntu/Developer/Libraries/swift/usr/bin/clang-10" -cc1 -triple aarch64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name cxx_lib.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=non-leaf -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -target-cpu generic -target-feature +neon -target-abi aapcs -fallow-half-arguments-and-returns -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -v -resource-dir /home/ubuntu/Developer/Libraries/swift/usr/lib/clang/10.0.0 -dependency-file /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/sources/cxx_lib.cpp.d -skip-unused-modulemap-deps -MT dependencies -sys-header-deps -D SWIFT_PACKAGE=1 -D DEBUG=1 -I /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include -I /usr/local/include/opencv4 -internal-isystem /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9 -internal-isystem /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9 -internal-isystem /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9 -internal-isystem /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9/backward -internal-isystem /usr/local/include -internal-isystem /home/ubuntu/Developer/Libraries/swift/usr/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/aarch64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -std=c++1z -fdeprecated-macro -fdebug-compilation-dir /home/ubuntu/Developer/OpenCVSwiftCXX -ferror-limit 19 -fmessage-length 0 -fblocks -fno-signed-char -fgnuc-version=4.2.1 -fmodules -fimplicit-module-maps -fmodules-cache-path=/home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -fmodule-name=CXXWrapper -fmodules-validate-system-headers -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -faddrsig -o /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/sources/cxx_lib.cpp.o -x c++ /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/sources/cxx_lib.cpp
clang version 10.0.0 (https://github.com/apple/llvm-project.git 3093af41dd65ad466dcd5603e9289244edfee4f5)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/ubuntu/Developer/Libraries/swift/usr/bin
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/aarch64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
clang-10: warning: -lopencv_core: 'linker' input unused [-Wunused-command-line-argument]
 "/home/ubuntu/Developer/Libraries/swift/usr/bin/clang-10" -cc1 -triple aarch64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name cxx_bridge.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=non-leaf -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -target-cpu generic -target-feature +neon -target-abi aapcs -fallow-half-arguments-and-returns -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -v -resource-dir /home/ubuntu/Developer/Libraries/swift/usr/lib/clang/10.0.0 -dependency-file /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/cxx_bridge.cpp.d -skip-unused-modulemap-deps -MT dependencies -sys-header-deps -D SWIFT_PACKAGE=1 -D DEBUG=1 -I /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include -I /usr/local/include/opencv4 -internal-isystem /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9 -internal-isystem /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9 -internal-isystem /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9 -internal-isystem /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9/backward -internal-isystem /usr/local/include -internal-isystem /home/ubuntu/Developer/Libraries/swift/usr/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/aarch64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -std=c++1z -fdeprecated-macro -fdebug-compilation-dir /home/ubuntu/Developer/OpenCVSwiftCXX -ferror-limit 19 -fmessage-length 0 -fblocks -fno-signed-char -fgnuc-version=4.2.1 -fmodules -fimplicit-module-maps -fmodules-cache-path=/home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -fmodule-name=CXXWrapper -fmodules-validate-system-headers -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -faddrsig -o /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/cxx_bridge.cpp.o -x c++ /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/cxx_bridge.cpp
clang -cc1 version 10.0.0 based upon LLVM 10.0.0git default target aarch64-unknown-linux-gnu
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9"
#include "..." search starts here:
#include <...> search starts here:
 /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include
 /usr/local/include/opencv4
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9/backward
 /usr/local/include
 /home/ubuntu/Developer/Libraries/swift/usr/lib/clang/10.0.0/include
 /usr/include/aarch64-linux-gnu
 /usr/include
End of search list.
clang -cc1 version 10.0.0 based upon LLVM 10.0.0git default target aarch64-unknown-linux-gnu
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9"
#include "..." search starts here:
#include <...> search starts here:
 /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include
 /usr/local/include/opencv4
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9/backward
 /usr/local/include
 /home/ubuntu/Developer/Libraries/swift/usr/lib/clang/10.0.0/include
 /usr/include/aarch64-linux-gnu
 /usr/include
End of search list.
clang -cc1 version 10.0.0 based upon LLVM 10.0.0git default target aarch64-unknown-linux-gnu
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9"
#include "..." search starts here:
#include <...> search starts here:
 /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include
 /usr/local/include/opencv4
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9/backward
 /usr/local/include
 /home/ubuntu/Developer/Libraries/swift/usr/lib/clang/10.0.0/include
 /usr/include/aarch64-linux-gnu
 /usr/include
End of search list.
clang -cc1 version 10.0.0 based upon LLVM 10.0.0git default target aarch64-unknown-linux-gnu
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9"
#include "..." search starts here:
#include <...> search starts here:
 /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include
 /usr/local/include/opencv4
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/aarch64-linux-gnu/c++/9
 /usr/lib/gcc/aarch64-linux-gnu/9/../../../../include/c++/9/backward
 /usr/local/include
 /home/ubuntu/Developer/Libraries/swift/usr/lib/clang/10.0.0/include
 /usr/include/aarch64-linux-gnu
 /usr/include
End of search list.
/home/ubuntu/Developer/Libraries/swift/usr/bin/swiftc -module-name OpenCVSwiftCXX -incremental -emit-dependencies -emit-module -emit-module-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.swiftmodule -output-file-map /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/output-file-map.json -c /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/OpenCVSwiftCXX/main.swift -I /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug -target aarch64-unknown-linux-gnu -swift-version 5 -enable-batch-mode -index-store-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/index/store -Onone -enable-testing -g -j4 -DSWIFT_PACKAGE -DDEBUG -Xcc -fmodule-map-file=/home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/module.modulemap -I /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include -module-cache-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -parseable-output -color-diagnostics
/home/ubuntu/Developer/Libraries/swift/usr/bin/swift -frontend -c -primary-file /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/OpenCVSwiftCXX/main.swift -emit-module-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/main~partial.swiftmodule -emit-module-doc-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/main~partial.swiftdoc -emit-module-source-info-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/main~partial.swiftsourceinfo -emit-dependencies-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/main.d -emit-reference-dependencies-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/main.swiftdeps -target aarch64-unknown-linux-gnu -Xllvm -aarch64-use-tbi -disable-objc-interop -I /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug -I /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include -color-diagnostics -enable-testing -g -module-cache-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/module.modulemap -module-name OpenCVSwiftCXX -o /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/main.swift.o -index-store-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/index/store -index-system-modules
/home/ubuntu/Developer/Libraries/swift/usr/bin/swift -frontend -merge-modules -emit-module /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/main~partial.swiftmodule -parse-as-library -sil-merge-partial-modules -disable-diagnostic-passes -disable-sil-perf-optzns -target aarch64-unknown-linux-gnu -Xllvm -aarch64-use-tbi -disable-objc-interop -I /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug -I /home/ubuntu/Developer/OpenCVSwiftCXX/Sources/CXXWrapper/include -color-diagnostics -enable-testing -g -module-cache-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/CXXWrapper.build/module.modulemap -emit-module-doc-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.swiftdoc -emit-module-source-info-path /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.swiftsourceinfo -module-name OpenCVSwiftCXX -o /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.swiftmodule
/home/ubuntu/Developer/Libraries/swift/usr/bin/swiftc -modulewrap /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.swiftmodule -o /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.build/OpenCVSwiftCXX.swiftmodule.o -target aarch64-unknown-linux-gnu
/home/ubuntu/Developer/Libraries/swift/usr/bin/swiftc -lstdc++ -L /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug -o /home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX -module-name OpenCVSwiftCXX -emit-executable -Xlinker '-rpath=$ORIGIN' @/home/ubuntu/Developer/OpenCVSwiftCXX/.build/aarch64-unknown-linux-gnu/debug/OpenCVSwiftCXX.product/Objects.LinkFileList -target aarch64-unknown-linux-gnu -L /home/ubuntu/Developer/Libraries/swift/usr/lib
Sources/CXXWrapper/sources/cxx_lib.cpp:16: error: undefined reference to 'cv::Mat::Mat()'
Sources/CXXWrapper/sources/cxx_lib.cpp:17: error: undefined reference to 'cv::Mat::~Mat()'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

✅ その2 - 解決: undefined reference to 'cv::Mat::Mat()'

swift のリンカーオプションの指定方法が間違っていた。-Xlinker を用いて指定する。

$ swift run -Xcxx=-I/usr/local/include/opencv4 -Xlinker=-L/usr/local/lib -Xlinker=-lopencv_core
[6/6] Linking OpenCVSwiftCXX
Date: 2021年1月10日日曜日 18時22分57秒 日本標準時
Linux ubuntu 5.4.0-1026-raspi #29-Ubuntu SMP PREEMPT Mon Dec 14 17:01:16 UTC 2020 aarch64 aarch64 aarch64 GNU/Linux
OpenCV version : 4.5.1
Major version : 4
Minor version : 5
Subminor version : 1

Swift Developers Japan の Discord で @katei に教えていただきました m(_ _)m

このスクラップは2021/01/15にクローズされました