我正在尝试编译网站上给出的示例问题(对于 C++)并且遇到了一个问题:NMead.cpp 是我的程序。我使用 g++ 编译它
g++ -g -O2 -I/usr/local/Cellar/nlopt/2.4.2/include -L/usr/local/Cellar/nlopt/2.4.2/lib -o NMead -lm -lnlopt NMead.cpp -v
生成的消息是:
Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin14/4.9.3/lto-wrapper Target: x86_64-apple-darwin14 Configured with: /opt/local/var/macports/build/_opt_mports_dports_lang_gcc49/gcc49/work/gcc-4.9.3/configure
--prefix=/opt/local --build=x86_64-apple-darwin14 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc49 --includedir=/opt/local/include/gcc49 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.9 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.9 --with-gxx-include-dir=/opt/local/include/gcc49/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --disable-isl-version-check --with-cloog=/opt/local --disable-cloog-version-check --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --with-pkgversion='MacPorts gcc49 4.9.3_0' Thread model: posix gcc version 4.9.3 (MacPorts gcc49 4.9.3_0) COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.10.5' '-g' '-O2' '-I' '/usr/local/Cellar/nlopt/2.4.2/include' '-L/usr/local/Cellar/nlopt/2.4.2/lib' '-o' 'NMead' '-v' '-shared-libgcc' '-mtune=core2' /opt/local/libexec/gcc/x86_64-apple-darwin14/4.9.3/cc1plus -quiet -v
-I /usr/local/Cellar/nlopt/2.4.2/include -D__DYNAMIC__ NMead.cpp -fPIC -feliminate-unused-debug-symbols -quiet -dumpbase NMead.cpp -mmacosx-version-min=10.10.5 -mtune=core2 -auxbase NMead -g -O2 -version -o /var/folders/zs/t_yzdt597z3cfwlr36hr78gw002vb6/T//cc5WiU5P.s GNU C++ (MacPorts gcc49 4.9.3_0) version 4.9.3 (x86_64-apple-darwin14) compiled by GNU C version 4.9.3, GMP version 6.0.0, MPFR version
3.1.3, MPC version 1.0.3 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring nonexistent directory "/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3/../../../../../x86_64-apple-darwin14/include"
#include "..." search starts here:
#include <...> search starts here: /usr/local/Cellar/nlopt/2.4.2/include /opt/local/include/gcc49/c++/ /opt/local/include/gcc49/c++//x86_64-apple-darwin14 /opt/local/include/gcc49/c++//backward /opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3/include /opt/local/include /opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3/include-fixed /usr/include /System/Library/Frameworks /Library/Frameworks End of search list. GNU C++ (MacPorts gcc49 4.9.3_0) version 4.9.3 (x86_64-apple-darwin14) compiled by GNU C version 4.9.3, GMP version
6.0.0, MPFR version 3.1.3, MPC version 1.0.3 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 0ca13fbc9617161ec7a95e943acdf57e COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.10.5' '-g' '-O2' '-I' '/usr/local/Cellar/nlopt/2.4.2/include' '-L/usr/local/Cellar/nlopt/2.4.2/lib' '-o' 'NMead' '-v' '-shared-libgcc' '-mtune=core2' /opt/local/bin/as -v -I /usr/local/Cellar/nlopt/2.4.2/include -arch x86_64
-force_cpusubtype_ALL -o /var/folders/zs/t_yzdt597z3cfwlr36hr78gw002vb6/T//cclPMJVL.o /var/folders/zs/t_yzdt597z3cfwlr36hr78gw002vb6/T//cc5WiU5P.s Apple Inc version cctools-870, GNU assembler version 1.38 COMPILER_PATH=/opt/local/libexec/gcc/x86_64-apple-darwin14/4.9.3/:/opt/local/libexec/gcc/x86_64-apple-darwin14/4.9.3/:/opt/local/libexec/gcc/x86_64-apple-darwin14/:/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3/:/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/ LIBRARY_PATH=/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3/:/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3/../../../:/usr/lib/ COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.10.5' '-g' '-O2' '-I' '/usr/local/Cellar/nlopt/2.4.2/include' '-L/usr/local/Cellar/nlopt/2.4.2/lib' '-o' 'NMead' '-v' '-shared-libgcc' '-mtune=core2' /opt/local/libexec/gcc/x86_64-apple-darwin14/4.9.3/collect2 -dynamic
-arch x86_64 -macosx_version_min 10.10.5 -weak_reference_mismatches non-weak -o NMead -L/usr/local/Cellar/nlopt/2.4.2/lib
-L/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3 -L/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3/../../.. -lnlopt /var/folders/zs/t_yzdt597z3cfwlr36hr78gw002vb6/T//cclPMJVL.o -lstdc++ -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v -idsym -dsym collect2 version 4.9.3 /opt/local/bin/ld -dynamic -arch x86_64 -macosx_version_min 10.10.5 -weak_reference_mismatches non-weak
-o NMead -L/usr/local/Cellar/nlopt/2.4.2/lib -L/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3 -L/opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3/../../.. -lnlopt /var/folders/zs/t_yzdt597z3cfwlr36hr78gw002vb6/T//cclPMJVL.o -lstdc++ -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v @(#)PROGRAM:ld PROJECT:ld64-242 configured to support archs: i386 x86_64 x86_64h armv6 armv7 armv7s armv7m armv7k arm64 Library search paths: /usr/local/Cellar/nlopt/2.4.2/lib /opt/local/lib/gcc49/gcc/x86_64-apple-darwin14/4.9.3 /opt/local/lib/gcc49 /usr/lib /usr/local/lib Framework search paths: /Library/Frameworks/ /System/Library/Frameworks/ /opt/local/bin/nm -n /var/folders/zs/t_yzdt597z3cfwlr36hr78gw002vb6/T//cclPMJVL.o dsymutil NMead
当我尝试运行生成的可执行文件时:
./NMead
我收到以下错误:
dyld:库未加载:/usr/local/opt/nlopt/lib/libnlopt_cxx.0.dylib 引用自:/Users/dlekshmi/Documents/workspace/TUTORIALS/./NMead 原因:找不到图像 Trace/BPT 陷阱:5
我怀疑出于某种原因,它正在寻找图书馆的错误位置。我跑了
otool -L ./NMead
并且生成的消息表明它确实是:
./NMead:/usr/local/opt/nlopt/lib/libnlopt_cxx.0.dylib(兼容版本9.0.0,当前版本9.2.0)/opt/local/lib/libgcc/libstdc++.6.dylib(兼容版本7.0.0, 当前版本 7.21.0) /usr/lib/libSystem.B.dylib (兼容版本 1.0.0, 当前版本 1213.0.0) /opt/local/lib/libgcc/libgcc_s.1.dylib (兼容版本1.0.0,当前版本 1.0.0)
我是 C++ 和 nlopt 的新手,我不确定我做错了什么。