Reputation: 317
The problem happens when I use "new" to define an array of size n when the n will be read-in when the program runs: x=new double[n]; If I delete the above line of code the program compiles successfully. I was told that I did not make any mistake in the grammar and the code can compile successfully using VC. I use mac and I suspect that this is due to the makefile so I put the error message and the makefile here. Does anyone know how to fix this? Thanks.
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
The 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)
The following is a minimal example which gives the same kind of error message:
#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;
}
The output of g++ -v is the following:
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)
The output of which g++ is the following:
client-104-39-60-181:~/Desktop/Desttop/work/C_programing/Test_6_23_2016]which g++
/opt/local/bin/g++
Upvotes: 0
Views: 734
Reputation: 126203
The problem would seem to be that you're getting the wrong libstdc++ version for the version of g++ that you are using. Best guess is that it is coming from the -L/usr/lib
or -L/usr/local/lib
options that you are explicitly adding to the link command line (where they will be searched before any built-in directories), so try removing them from LFLAGS
(they should be searched by default, but after any compiler-specific directories). If that doesn't work, try adding -L/opt/local/lib
to LFLAGS, or search in /opt/local
for libstdc++.*
and link it explicitly.
Upvotes: 1