发生PIE链接错误时,无法使用针对“ .bss”的重定位R_X86_64_32S

时间:2018-08-28 20:12:23

标签: c++ gcc makefile linker nvcc

我正在尝试在CUDA支持下编译AccNEAT项目。在没有CUDA支持的情况下进行编译时,它工作正常。但是,当我使用CUDA支持进行编译时,会出现链接器错误。要编译项目,我的环境是64位的Ubuntu 18.04 LTS,带有GCC-4.8和NVCC 6.0。

链接器错误:

/usr/bin/x86_64-linux-gnu-ld: obj/cu/network/cuda/cudanetwork.o: relocation R_X86_64_32S against `.bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: obj/cu/cxx/experiments/maze/mazeevaluator.o: relocation R_X86_64_32S against `.bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: obj/cu/cxx/experiments/static/staticevaluator.o: relocation R_X86_64_32S against `.bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Makefile:49: recipe for target 'neat' failed
make: *** [neat] Error 1

编译器建议添加一个-fPIC选项,但是我不知道将其放在Makefile中的确切位置。我尝试使用-XCompiler之类的-XCompiler "-fPIC..."选项添加它,但随后在编译时出现此错误:

obj/cu/cxx/experiments/maze/mazeevaluator.o: In function `NEAT::create_config(NEAT::Config*&, unsigned long&)':
/home/ner/Projekty/accneat-master-cuda/src/experiments/maze/mazeevaluator.cxx:158: undefined reference to `NEAT::find_resource(std::string const&)'
/home/ner/Projekty/accneat-master-cuda/src/experiments/maze/mazeevaluator.cxx:158: undefined reference to `NEAT::parse_map(std::string)'
collect2: error: ld returned 1 exit status
Makefile:49: recipe for target 'neat' failed
make: *** [neat] Error 1

“重定位”错误是什么意思,有什么方法可以解决此问题?

我的Makefile:

include Makefile.conf

CC_CUDA=nvcc -DENABLE_CUDA ${NVCC_FLAGS} -arch=sm_13 --debug --compiler-bindir ${PFM_NVCC_CCBIN} -Xcompiler "${OPT} ${INCLUDES} ${OPENMP} -I/usr/local/cuda/include "

INCLUDES=$(patsubst %,-I%,$(shell find src -type d))
SOURCES=$(shell find src -name "*.cpp")
CXX_SOURCES=$(shell find src -name "*.cxx")

OBJECTS=${SOURCES:src/%.cpp=obj/cpp/%.o}

LIBS=-lgomp
DEFINES=

ifeq (${ENABLE_CUDA}, true)
    CUDA_SOURCES=$(shell find src -name "*.cu")
    CUDA_OBJECTS=${CUDA_SOURCES:src/%.cu=obj/cu/%.o}
    CUDA_OBJECTS+=${CXX_SOURCES:src/%.cxx=obj/cu/cxx/%.o}
    LIBS+=-lcudart
    DEFINES+=-DENABLE_CUDA
else
    OBJECTS+=${CXX_SOURCES:src/%.cxx=obj/cpp/cxx/%.o}
endif

DEPENDS=${OBJECTS:%.o=%.d}
DEPENDS+=${CUDA_OBJECTS:%.o=%.d}

ifeq (${DEVMODE}, true)
    OPT=-O0
    #OPENMP=-fopenmp
    MISC_FLAGS=
    NVCC_FLAGS=-G -g
else
    OPT=-O3
    OPENMP=-fopenmp
    MISC_FLAGS=-Werror
endif

CC_FLAGS=-Wall ${DEFINES} ${MISC_FLAGS} ${PROFILE} ${INCLUDES} ${OPENMP} ${OPT} -c -g -gdwarf-3
.PHONY: clean default

default: ./neat

clean:
    rm -rf obj
    rm -f ./neat
    rm -f src/util/std.h.gch

./neat: ${OBJECTS} ${CUDA_OBJECTS}
    g++ ${PROFILE} ${OBJECTS} ${CUDA_OBJECTS} ${PFM_LD_FLAGS} ${LIBS} -o $@

src/util/std.h.gch: src/util/std.h Makefile.conf Makefile
    g++ ${CC_FLAGS} -std=c++11 $< -o $@

ifeq (${ENABLE_CUDA}, true)

obj/cu/cxx/%.o: src/%.cxx Makefile.conf Makefile
    @mkdir -p $(shell dirname $@)
    ${CC_CUDA} -c -x cu $< -o $@

obj/cu/cxx/%.d: src/%.cxx
    @mkdir -p $(dir $@)
    @${CC_CUDA} -M -x cu $< > $@.tmp
    @cat $@.tmp | sed 's,.*\.o[[:space:]]*:,$@:,g' | sed 's,\.d,\.o,' > $@
    @rm $@.tmp

obj/cu/%.o: src/%.cu Makefile.conf Makefile
    @mkdir -p $(shell dirname $@)
    ${CC_CUDA} -c $< -o $@

obj/cu/%.d: src/%.cu
    @mkdir -p $(dir $@)
    @${CC_CUDA} -M $< > $@.tmp
    @cat $@.tmp | sed 's,.*\.o[[:space:]]*:,$@:,g' | sed 's,\.d,\.o,' > $@
    @rm $@.tmp

else
obj/cpp/cxx/%.o: src/%.cxx Makefile.conf Makefile
    @mkdir -p $(shell dirname $@)
    g++ ${CC_FLAGS} -std=c++98 -MMD $< -o $@
endif

obj/cpp/%.o: src/%.cpp Makefile.conf Makefile src/util/std.h.gch
    @mkdir -p $(shell dirname $@)
    g++ ${CC_FLAGS} -std=c++11 -MMD $< -o $@

-include ${DEPENDS}

(我添加了一些小的更改以使其像include nvcc dir路径一样进行编译)。

构建命令:

g++  obj/cpp/experiments/maze/maze.o obj/cpp/experiments/static/regex.o obj/cpp/experiments/static/cfg.o obj/cpp/experiments/static/xor.o obj/cpp/experiments/static/sequence.o obj/cpp/experiments/experiment.o obj/cpp/innovgenome/innovation.o obj/cpp/innovgenome/trait.o obj/cpp/innovgenome/innovnodegene.o obj/cpp/innovgenome/innovlinkgene.o obj/cpp/innovgenome/innovgenome.o obj/cpp/innovgenome/innovgenomemanager.o obj/cpp/genomemanager.o obj/cpp/population.o obj/cpp/network/cpu/cpunetwork.o obj/cpp/species/speciesorganism.o obj/cpp/species/species.o obj/cpp/species/speciespopulation.o obj/cpp/util/timer.o obj/cpp/util/rng.o obj/cpp/util/util.o obj/cpp/util/resource.o obj/cpp/util/map.o obj/cpp/main.o obj/cpp/organism.o obj/cpp/neat.o obj/cu/network/cuda/cudanetwork.o obj/cu/cxx/experiments/maze/mazeevaluator.o obj/cu/cxx/experiments/static/staticevaluator.o  -lgomp -lcudart -o neat

编辑1:添加了完整的构建日志:

nerexis@nerexis-GE70-0NC-GE70-0ND:~/Projekty/accneat-master-cuda$ make clean
rm -rf obj
rm -f ./neat
rm -f src/util/std.h.gch
nerexis@nerexis-GE70-0NC-GE70-0ND:~/Projekty/accneat-master-cuda$ make
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 src/util/std.h -o src/util/std.h.gch
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/experiments/maze/maze.cpp -o obj/cpp/experiments/maze/maze.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/experiments/static/regex.cpp -o obj/cpp/experiments/static/regex.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/experiments/static/cfg.cpp -o obj/cpp/experiments/static/cfg.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/experiments/static/xor.cpp -o obj/cpp/experiments/static/xor.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/experiments/static/sequence.cpp -o obj/cpp/experiments/static/sequence.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/experiments/experiment.cpp -o obj/cpp/experiments/experiment.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/innovgenome/innovation.cpp -o obj/cpp/innovgenome/innovation.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/innovgenome/trait.cpp -o obj/cpp/innovgenome/trait.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/innovgenome/innovnodegene.cpp -o obj/cpp/innovgenome/innovnodegene.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/innovgenome/innovlinkgene.cpp -o obj/cpp/innovgenome/innovlinkgene.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/innovgenome/innovgenome.cpp -o obj/cpp/innovgenome/innovgenome.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/innovgenome/innovgenomemanager.cpp -o obj/cpp/innovgenome/innovgenomemanager.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/genomemanager.cpp -o obj/cpp/genomemanager.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/population.cpp -o obj/cpp/population.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/network/cpu/cpunetwork.cpp -o obj/cpp/network/cpu/cpunetwork.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/species/speciesorganism.cpp -o obj/cpp/species/speciesorganism.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/species/species.cpp -o obj/cpp/species/species.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/species/speciespopulation.cpp -o obj/cpp/species/speciespopulation.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/util/timer.cpp -o obj/cpp/util/timer.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/util/rng.cpp -o obj/cpp/util/rng.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/util/util.cpp -o obj/cpp/util/util.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/util/resource.cpp -o obj/cpp/util/resource.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/util/map.cpp -o obj/cpp/util/map.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/main.cpp -o obj/cpp/main.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/organism.cpp -o obj/cpp/organism.o
g++ -Wall  -DENABLE_CUDA -Werror  -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -O3 -c -g -gdwarf-3 -fPIC -std=c++11 -MMD src/neat.cpp -o obj/cpp/neat.o
nvcc -DENABLE_CUDA  -arch=sm_13 --debug --compiler-bindir gcc-4.8 -Xcompiler "-O3 -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -I/usr/local/cuda/include -L/usrlocal/cuda/lib64" -c src/network/cuda/cudanetwork.cu -o obj/cu/network/cuda/cudanetwork.o
nvcc -DENABLE_CUDA  -arch=sm_13 --debug --compiler-bindir gcc-4.8 -Xcompiler "-O3 -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -I/usr/local/cuda/include -L/usrlocal/cuda/lib64" -c -x cu src/experiments/maze/mazeevaluator.cxx -o obj/cu/cxx/experiments/maze/mazeevaluator.o
nvcc -DENABLE_CUDA  -arch=sm_13 --debug --compiler-bindir gcc-4.8 -Xcompiler "-O3 -Isrc -Isrc/experiments -Isrc/experiments/maze -Isrc/experiments/static -Isrc/innovgenome -Isrc/network -Isrc/network/cuda -Isrc/network/cpu -Isrc/species -Isrc/util -fopenmp -I/usr/local/cuda/include -L/usrlocal/cuda/lib64" -c -x cu src/experiments/static/staticevaluator.cxx -o obj/cu/cxx/experiments/static/staticevaluator.o
g++  obj/cpp/experiments/maze/maze.o obj/cpp/experiments/static/regex.o obj/cpp/experiments/static/cfg.o obj/cpp/experiments/static/xor.o obj/cpp/experiments/static/sequence.o obj/cpp/experiments/experiment.o obj/cpp/innovgenome/innovation.o obj/cpp/innovgenome/trait.o obj/cpp/innovgenome/innovnodegene.o obj/cpp/innovgenome/innovlinkgene.o obj/cpp/innovgenome/innovgenome.o obj/cpp/innovgenome/innovgenomemanager.o obj/cpp/genomemanager.o obj/cpp/population.o obj/cpp/network/cpu/cpunetwork.o obj/cpp/species/speciesorganism.o obj/cpp/species/species.o obj/cpp/species/speciespopulation.o obj/cpp/util/timer.o obj/cpp/util/rng.o obj/cpp/util/util.o obj/cpp/util/resource.o obj/cpp/util/map.o obj/cpp/main.o obj/cpp/organism.o obj/cpp/neat.o obj/cu/network/cuda/cudanetwork.o obj/cu/cxx/experiments/maze/mazeevaluator.o obj/cu/cxx/experiments/static/staticevaluator.o  -lgomp -lcudart -o neat
/usr/bin/x86_64-linux-gnu-ld: obj/cu/network/cuda/cudanetwork.o: relocation R_X86_64_32S against `.bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: obj/cu/cxx/experiments/maze/mazeevaluator.o: relocation R_X86_64_32S against `.bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: obj/cu/cxx/experiments/static/staticevaluator.o: relocation R_X86_64_32S against `.bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Makefile:49: recipe for target 'neat' failed
make: *** [neat] Error 1

编辑2: 生成文件:

include Makefile.conf
CC_CUDA=nvcc -DENABLE_CUDA ${NVCC_FLAGS} -arch=sm_20 --debug --compiler-bindir ${PFM_NVCC_CCBIN} -Xcompiler "${OPT} ${INCLUDES} ${OPENMP} -I/usr/local/cuda/include -L/usr/local/cuda/lib64 "

INCLUDES=$(patsubst %,-I%,$(shell find src -type d))
SOURCES=$(shell find src -name "*.cpp")
CXX_SOURCES=$(shell find src -name "*.cxx")

OBJECTS=${SOURCES:src/%.cpp=obj/cpp/%.o}

LIBS=-lgomp
DEFINES=

ifeq (${ENABLE_CUDA}, true)
    CUDA_SOURCES=$(shell find src -name "*.cu")
    CUDA_OBJECTS=${CUDA_SOURCES:src/%.cu=obj/cu/%.o}
    CUDA_OBJECTS+=${CXX_SOURCES:src/%.cxx=obj/cu/cxx/%.o}
    LIBS+=-lcudart
    DEFINES+=-DENABLE_CUDA
else
    OBJECTS+=${CXX_SOURCES:src/%.cxx=obj/cpp/cxx/%.o}
endif

DEPENDS=${OBJECTS:%.o=%.d}
DEPENDS+=${CUDA_OBJECTS:%.o=%.d}

ifeq (${DEVMODE}, true)
    OPT=-O0
    #OPENMP=-fopenmp
    MISC_FLAGS=
    NVCC_FLAGS=-G -g
else
    OPT=-O3
    OPENMP=-fopenmp
    MISC_FLAGS=-Werror
    NVCC_FLAGS=--relocatable-device-code=true --dont-use-profile -ldir /usr/local/cuda/nvvm/libdevice 
endif

CC_FLAGS=-Wall ${DEFINES} ${MISC_FLAGS} ${PROFILE} ${INCLUDES} ${OPENMP} ${OPT} -c -g -gdwarf-3 -fPIC
.PHONY: clean default

default: ./neat

clean:
    rm -rf obj
    rm -f ./neat
    rm -f src/util/std.h.gch

./neat: ${OBJECTS} ${CUDA_OBJECTS}
    g++ ${PROFILE} ${OBJECTS} ${CUDA_OBJECTS} ${PFM_LD_FLAGS} -fPIC ${LIBS} -o $@

src/util/std.h.gch: src/util/std.h Makefile.conf Makefile
    g++ ${CC_FLAGS} -std=c++11 $< -o $@

ifeq (${ENABLE_CUDA}, true)

obj/cu/cxx/%.o: src/%.cxx Makefile.conf Makefile
    @mkdir -p $(shell dirname $@)
    ${CC_CUDA} -c -x cu $< -o $@

obj/cu/cxx/%.d: src/%.cxx
    @mkdir -p $(dir $@)
    @${CC_CUDA} -M -x cu $< > $@.tmp
    @cat $@.tmp | sed 's,.*\.o[[:space:]]*:,$@:,g' | sed 's,\.d,\.o,' > $@
    @rm $@.tmp

obj/cu/%.o: src/%.cu Makefile.conf Makefile
    @mkdir -p $(shell dirname $@)
    ${CC_CUDA} -c $< -o $@

obj/cu/%.d: src/%.cu
    @mkdir -p $(dir $@)
    @${CC_CUDA} -M $< > $@.tmp
    @cat $@.tmp | sed 's,.*\.o[[:space:]]*:,$@:,g' | sed 's,\.d,\.o,' > $@
    @rm $@.tmp

else
obj/cpp/cxx/%.o: src/%.cxx Makefile.conf Makefile
    @mkdir -p $(shell dirname $@)
    g++ ${CC_FLAGS} -std=c++98 -MMD $< -o $@
endif

obj/cpp/%.o: src/%.cpp Makefile.conf Makefile src/util/std.h.gch
    @mkdir -p $(shell dirname $@)
    g++ ${CC_FLAGS} -std=c++11 -MMD $< -o $@

-include ${DEPENDS}

构建日志(由于长度限制而结束):

g++  obj/cpp/experiments/maze/maze.o obj/cpp/experiments/static/regex.o obj/cpp/experiments/static/cfg.o obj/cpp/experiments/static/xor.o obj/cpp/experiments/static/sequence.o obj/cpp/experiments/experiment.o obj/cpp/innovgenome/innovation.o obj/cpp/innovgenome/trait.o obj/cpp/innovgenome/innovnodegene.o obj/cpp/innovgenome/innovlinkgene.o obj/cpp/innovgenome/innovgenome.o obj/cpp/innovgenome/innovgenomemanager.o obj/cpp/genomemanager.o obj/cpp/population.o obj/cpp/network/cpu/cpunetwork.o obj/cpp/species/speciesorganism.o obj/cpp/species/species.o obj/cpp/species/speciespopulation.o obj/cpp/util/timer.o obj/cpp/util/rng.o obj/cpp/util/util.o obj/cpp/util/resource.o obj/cpp/util/map.o obj/cpp/main.o obj/cpp/organism.o obj/cpp/neat.o obj/cu/network/cuda/cudanetwork.o obj/cu/cxx/experiments/maze/mazeevaluator.o obj/cu/cxx/experiments/static/staticevaluator.o  -fPIC -lgomp -lcudart -o neat
/usr/bin/x86_64-linux-gnu-ld: obj/cu/network/cuda/cudanetwork.o: relocation R_X86_64_32S against symbol `_ZTVN4NEAT11CudaNetworkE' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: obj/cu/cxx/experiments/maze/mazeevaluator.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: obj/cu/cxx/experiments/static/staticevaluator.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Makefile:50: recipe for target 'neat' failed
make: *** [neat] Error 1

编辑3:当我将选项-fPIC -shared添加到-Xcompiler选项以及CC_FLAGS和./neat:g ++行时,我能够进行编译。 运行编译的二进制文件时,出现分段错误错误。 valgrind程序的其他信息:

==14367== Jump to the invalid address stated on the next line
==14367==    at 0x1C296: ???
==14367==    by 0x12506B: operator<< <std::char_traits<char> > (ostream:561)
==14367==    by 0x12506B: cmp(NEAT::InnovationId const&, NEAT::InnovationId const&) [clone .part.5] (innovation.cpp:43)
==14367==  Address 0x1c296 is not stack'd, malloc'd or (recently) free'd
==14367== 
==14367== 
==14367== Process terminating with default action of signal 11 (SIGSEGV)
==14367==  Bad permissions for mapped region at address 0x1C296
==14367==    at 0x1C296: ???
==14367==    by 0x12506B: operator<< <std::char_traits<char> > (ostream:561)
==14367==    by 0x12506B: cmp(NEAT::InnovationId const&, NEAT::InnovationId const&) [clone .part.5] (innovation.cpp:43)
==14367== 

2 个答案:

答案 0 :(得分:0)

从以下位置更改CC_FLAGS(编译选项)的设置:

CC_FLAGS=-Wall ${DEFINES} ${MISC_FLAGS} ${PROFILE} ${INCLUDES} ${OPENMP} ${OPT} -c -g -gdwarf-3

收件人:

CC_FLAGS=-Wall ${DEFINES} ${MISC_FLAGS} ${PROFILE} ${INCLUDES} ${OPENMP} ${OPT} -c -g -gdwarf-3 -fPIC

似乎有可能解决这种特殊的连接故障。

答案 1 :(得分:0)

我在this thread中介绍了一个建议,建议在nvcc编译器选项中添加以下内容,这对我有用:

nvcc --compiler-options -fPIC ...

我才刚刚了解到PIC代表位置无关代码(意味着可执行文件可以加载到内存中的任何位置)。编译PIC可执行文件时,所有引用的库和对象也都必须是PIC(我认为),包括Cuda代码。因此,在我的情况下,需要将fPIC选项传递给nvcc而不是g ++。