使用 g++ 编译 C++ 程序时,体系结构的未定义符号 x86_64
Undefined symbols for architecture x86_64 whem compiling a C++ program using g++
当我使用"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)
which g++ 的输出如下:
client-104-39-60-181:~/Desktop/Desttop/work/C_programing/Test_6_23_2016]which g++
/opt/local/bin/g++
问题似乎是您获得的 libstdc++ 版本与您正在使用的 g++ 版本不符。最好的猜测是它来自您明确添加到 link 命令行的 -L/usr/lib
或 -L/usr/local/lib
选项(将在任何内置目录之前搜索它们),所以尝试从 LFLAGS
中删除它们(默认情况下应该搜索它们,但在任何特定于编译器的目录之后)。如果这不起作用,请尝试将 -L/opt/local/lib
添加到 LFLAGS,或在 /opt/local
中明确搜索 libstdc++.*
和 link。
当我使用"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)
which g++ 的输出如下:
client-104-39-60-181:~/Desktop/Desttop/work/C_programing/Test_6_23_2016]which g++
/opt/local/bin/g++
问题似乎是您获得的 libstdc++ 版本与您正在使用的 g++ 版本不符。最好的猜测是它来自您明确添加到 link 命令行的 -L/usr/lib
或 -L/usr/local/lib
选项(将在任何内置目录之前搜索它们),所以尝试从 LFLAGS
中删除它们(默认情况下应该搜索它们,但在任何特定于编译器的目录之后)。如果这不起作用,请尝试将 -L/opt/local/lib
添加到 LFLAGS,或在 /opt/local
中明确搜索 libstdc++.*
和 link。