架构的未定义符号x86_64使用g ++编译C ++程序

时间:2016-06-23 20:17:53

标签: c++ macos makefile new-operator

当我在程序运行时读入n时,使用“new”来定义大小为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

makefile:

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++

1 个答案:

答案 0 :(得分:1)

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