c++ - 使用 g++ 编译 C++ 程序时体系结构 x86_64 的 undefined symbol

标签 c++ macos makefile new-operator

当我使用“new”定义大小为n的数组时,当程序运行时n将被读入时,就会出现问题:x=new double[n]; 如果我删除上面的代码行,程序将成功编译。 我被告知我在语法上没有犯任何错误,并且代码可以使用 VC 成功编译。我使用 mac,我怀疑这是由于 makefile 造成的,所以我将错误消息和 makefile 放在这里。有谁知道如何解决这一问题?谢谢。

client-104-39-60-181:~/Desktop/Desttop/work/C_programing/Test_6_21_2016]make
g++ -Wall   -W -g -std=c++14 -I/usr/include -I/usr/include/GL -I/usr/X11R6/include -I/usr/X11R6/include/GL main.cpp -c -o main.o
g++ -Wall   -W -g -std=c++14 -I/usr/include -I/usr/include/GL -I/usr/X11R6/include -I/usr/X11R6/include/GL mytest.cpp -c -o mytest.o
g++ -Wall   -W -g -std=c++14 -I/usr/include -I/usr/include/GL -I/usr/X11R6/include -I/usr/X11R6/include/GL run_Lorenz96.cpp -c -o run_Lorenz96.o
g++ main.o mytest.o run_Lorenz96.o -L/usr/lib -L/usr/X11R6/lib -L/usr/local/lib -lX11 -lGL -lGLU -lglut -lcblas -lclapack -o run
Undefined symbols for architecture x86_64:
  "___cxa_throw_bad_array_new_length", referenced from:
      run_Lorenz96::init()      in run_Lorenz96.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [run] Error 1

生成文件:

NAME        = run
CC      = g++
SRC     = main.cpp mytest.cpp run_Lorenz96.cpp
CFLAGS      = -Wall -W -g -std=c++14

IFLAGS= -I/usr/include -I/usr/include/GL -I/usr/X11R6/include -I/usr/X11R6/include/GL

LFLAGS= -L/usr/lib -L/usr/X11R6/lib -L/usr/local/lib -lX11 -lGL -lGLU -lglut -lcblas -lclapack

OBJ   = $(SRC:.cpp=.o)

all :       $(NAME)

$(NAME) :   $(OBJ)
        $(CC) $(OBJ) $(LFLAGS) -o $(NAME)

.cpp.o : 
        $(CC) $(CFLAGS) $(IFLAGS) $< -c -o $@

clean :
        $(RM) $(OBJ)
        $(RM) $(NAME) 

以下是给出相同类型错误消息的最小示例:

#include<iostream>
#include<math.h>
#include<vector>
#include<lapacke.h>


using namespace std;

int main(){
    int n;
    double* x;

    n=10;
    x=new double[n];

    return 0;

}

g++ -v 的输出如下:

client-104-39-60-181:~/Desktop/Desttop/work/C_programing/Test_6_23_2016]g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin13/5.4.0/lto-wrapper
Target: x86_64-apple-darwin13
Configured with: /opt/local/var/macports/build/_opt_mports_dports_lang_gcc5/gcc5/work/gcc-5.4.0/configure --prefix=/opt/local --build=x86_64-apple-darwin13 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc5 --includedir=/opt/local/include/gcc5 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-5 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-5 --with-gxx-include-dir=/opt/local/include/gcc5/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-build-config=bootstrap-debug --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 gcc5 5.4.0_0'
Thread model: posix
gcc version 5.4.0 (MacPorts gcc5 5.4.0_0) 

g++ 的输出如下:

client-104-39-60-181:~/Desktop/Desttop/work/C_programing/Test_6_23_2016]which g++
/opt/local/bin/g++

最佳答案

问题似乎是您所使用的 g++ 版本的 libstdc++ 版本错误。最好的猜测是它来自您显式添加到链接命令行的 -L/usr/lib-L/usr/local/lib 选项(它们将在任何内置目录之前被搜索),因此请尝试从 LFLAGS 中删除它们(默认情况下应该搜索它们,但在任何特定于编译器的目录之后)。如果这不起作用,请尝试将 -L/opt/local/lib 添加到 LFLAGS,或在 /opt/local 中搜索 libstdc++。* 并显式链接它。

关于c++ - 使用 g++ 编译 C++ 程序时体系结构 x86_64 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38001042/

相关文章:

android-ndk - 更改Android.mk后如何防止Android原生项目完全重建?

c++ - 带有现有 C/C++ 项目的 Eclipse CDT

c# - 带有 Directx 11 的 WPF

c++ - 为什么 g++ 使我的代码以不同于写入的顺序执行,我如何禁用此 "optimization"?

cocoa - 在 Cocoa OSX 中捕获双击

macos - 热衷于在 MAC OS X 中获取格式为 "en_US"的键盘布局语言和区域设置?

C++ 纯静态类

c++ - qDebug - 如何以位输出数据(二进制格式)

java - 如果通过双击 JNLP 文件启动 JAVA Web Start 应用程序,如何允许它访问 macOS Catalina 上的文件系统?

makefile - 如何将多行输入 (stdin) 传递到我在 Makefile 中执行的命令中?