当我使用"new“定义大小为n的数组时,当程序运行时n将被读取时,问题就会发生: x=new doublen;如果我删除上面的代码行,程序将成功编译。有人告诉我,我在语法上没有犯任何错误,代码可以用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 1The 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++发布于 2016-06-23 20:58:15
问题似乎是,您正在使用的g++版本获得了错误的g++版本。最好的猜测是,它来自于您要显式添加到链接命令行的-L/usr/lib或-L/usr/local/lib选项(在链接命令行中,它们将在任何内置目录之前被搜索),所以尝试从LFLAGS中删除它们(默认情况下应该搜索它们,但是在任何特定于编译器的目录之后)。如果这不起作用,尝试将-L/opt/local/lib添加到LFLAGS,或者在/opt/local中搜索libstdc++.*并显式链接它。
https://stackoverflow.com/questions/38001042
复制相似问题