Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 85383b7

Browse files
committed
(#4) Added helper function which produces correct linker flags for macOS and Linux
1 parent 7a15a84 commit 85383b7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

install/install.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const opencvBuild = require(`@nut-tree/opencv-build-${process.platform}`)
22
const child_process = require('child_process')
3+
const { basename } = require("path");
34
const fs = require('fs')
45
const log = require('npmlog')
56
const { resolvePath } = require('../lib/commons')
@@ -58,11 +59,23 @@ const defines = libsFoundInDir
5859
// : [resolvePath(opencvBuild.opencvInclude), resolvePath(opencvBuild.opencv4Include)]
5960
const includes = [resolvePath(opencvBuild.opencvInclude), resolvePath(opencvBuild.opencv4Include)]
6061

62+
// linkLib produces linker flags for GNU ld and BSD ld
63+
// It generates linker flags based on the libPath, which make dealing with version numbers in lib names easier
64+
// On Linux, it passes the full path via -l:/path/to/lib which links against the given file
65+
// On macOS it strips the *.dylib suffix and the lib prefix and passes the result to via -l
66+
// This results in e.g. -lopencv_world.4.1
67+
const linkLib = (lib) => {
68+
if (opencvBuild.isOSX()) {
69+
return `-l${basename(lib.libPath, ".dylib").replace("lib", "")}`;
70+
} else {
71+
return `-l:${basename(lib.libPath)}`;
72+
}
73+
}
6174
const libs = opencvBuild.isWin()
6275
? libsFoundInDir.map(lib => resolvePath(lib.libPath))
6376
// dynamically link libs if not on windows
6477
: ['-L' + libDir]
65-
.concat(libsFoundInDir.map(lib => '-lopencv_' + lib.opencvModule))
78+
.concat(libsFoundInDir.map(lib => linkLib(lib)))
6679
.concat('-Wl,-rpath,' + libDir)
6780

6881
console.log()

0 commit comments

Comments
 (0)