我已经看过很多次,但找不到有效的答案。我试图编译我的项目但仍然遇到一堆x86-64错误。我知道我可能会因为它有几个类似的问题(不适合我)而被推崇,但我需要弄明白。谢谢你的帮助。
这是我不断获得的makefile +错误。
LINK_FILES=`pkg-config opencv --cflags --libs`
CXX_FLAGS=-std=c++11 -stdlib=libstdc++ -std=c++0x -O3 -I/opt/local/include/opencv -I/opt/local/include
all: HW2
HW2: main.o Union_Find.o Kernals.o
g++ -o hw2 main.o Union_Find.o Kernals.o ${LINK_FILES}
main.o: main.cpp
g++ -c main.cpp ${CXX_FLAGS}
Union_Find.o: Union_Find.hpp Union_Find.cpp
g++ -c Union_Find.cpp ${CXX_FLAGS}
Kernals.o: Kernals.hpp Kernals.cu
nvcc -c Kernals.cu
clean:
rm *.o hw2
这些错误是我用这个makefile编译时得到的。
g++ -c main.cpp -std=c++11 -stdlib=libstdc++ -std=c++0x -O3 -I/opt/local/include/opencv -I/opt/local/include
g++ -c Union_Find.cpp -std=c++11 -stdlib=libstdc++ -std=c++0x -O3 -I/opt/local/include/opencv -I/opt/local/include
nvcc -c Kernals.cu
g++ -o hw2 main.o Union_Find.o Kernals.o `pkg-config opencv --cflags --libs`
Undefined symbols for architecture x86_64:
"cv::imread(std::string const&, int)", referenced from:
_main in main.o
"cv::imshow(std::string const&, cv::_InputArray const&)", referenced from:
_main in main.o
"cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)", referenced from:
_main in main.o
"std::basic_ios<char, std::char_traits<char> >::widen(char) const", referenced from:
_main in main.o
"std::ostream::put(char)", referenced from:
_main in main.o
"std::ostream::flush()", referenced from:
_main in main.o
"std::ostream::operator<<(int)", referenced from:
_main in main.o
"std::string::_Rep::_M_destroy(std::allocator<char> const&)", referenced from:
_main in main.o
"std::string::_Rep::_S_empty_rep_storage", referenced from:
_main in main.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
_main in main.o
"std::ios_base::Init::Init()", referenced from:
__GLOBAL__I_a in main.o
__GLOBAL__I_a in Union_Find.o
___cxx_global_var_init in Kernals.o
"std::ios_base::Init::~Init()", referenced from:
__GLOBAL__I_a in main.o
__GLOBAL__I_a in Union_Find.o
___cxx_global_var_init in Kernals.o
"std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)", referenced from:
_main in main.o
"std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)", referenced from:
_main in main.o
"std::_Rb_tree_decrement(std::_Rb_tree_node_base*)", referenced from:
std::_Rb_tree<int, std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > >, std::_Select1st<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > >, std::less<int>, std::allocator<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > > >::_M_insert_unique(std::_Rb_tree_iterator<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > >, std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > const&) in Union_Find.o
std::_Rb_tree<int, std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > >, std::_Select1st<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > >, std::less<int>, std::allocator<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > > >::_M_insert_unique(std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > const&) in Union_Find.o
"std::_Rb_tree_increment(std::_Rb_tree_node_base*)", referenced from:
std::_Rb_tree<int, std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > >, std::_Select1st<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > >, std::less<int>, std::allocator<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > > >::_M_insert_unique(std::_Rb_tree_iterator<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > >, std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > const&) in Union_Find.o
"std::__throw_length_error(char const*)", referenced from:
std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&) in Union_Find.o
"std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)", referenced from:
std::_Rb_tree<int, std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > >, std::_Select1st<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > >, std::less<int>, std::allocator<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > > >::_M_insert_unique(std::_Rb_tree_iterator<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > >, std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > const&) in Union_Find.o
std::_Rb_tree<int, std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > >, std::_Select1st<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > >, std::less<int>, std::allocator<std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > > >::_M_insert_unique(std::pair<int const, std::pair<cv::Vec<unsigned char, 3>, std::vector<int, std::allocator<int> > > > const&) in Union_Find.o
"std::cout", referenced from:
_main in main.o
"___cudaRegisterFatBinary", referenced from:
__sti____cudaRegisterAll_42_tmpxft_0000a8de_00000000_6_Kernals_cpp1_ii_f3b8c2ed() in Kernals.o
"___cudaRegisterFunction", referenced from:
__nv_cudaEntityRegisterCallback(void**) in Kernals.o
"___cudaUnregisterFatBinary", referenced from:
__cudaUnregisterBinaryUtil() in Kernals.o
"_cudaConfigureCall", referenced from:
callKernal(std::vector<int, std::allocator<int> >&, unsigned char*, int, int) in Kernals.o
"_cudaFree", referenced from:
callKernal(std::vector<int, std::allocator<int> >&, unsigned char*, int, int) in Kernals.o
"_cudaLaunch", referenced from:
cudaError (anonymous namespace)::cudaLaunch<char>(char*) in Kernals.o
"_cudaMalloc", referenced from:
callKernal(std::vector<int, std::allocator<int> >&, unsigned char*, int, int) in Kernals.o
"_cudaMemcpy", referenced from:
callKernal(std::vector<int, std::allocator<int> >&, unsigned char*, int, int) in Kernals.o
"_cudaSetupArgument", referenced from:
__device_stub__Z9firstPassPhiiPi(unsigned char*, int, int, int*) in Kernals.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [HW2] Error 1
当我使用cmake时,我使用此CMakeLists.txt文件:
project( hw2 )
cmake_minimum_required(VERSION 2.8)
add_definitions(-DHAVE_ALLOCA -DHAVE_ALLOCA_H -DHAVE_LIBPTHREAD -DHAVE_UNISTD_H)
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -stdlib=libstdc++")
#SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -stdlib=libstdc++")
SET(CUDA_HOST_COMPILER "/usr/bin/clang")
SET(CUDA_NVCC_FLAGS "-Xcompiler -stdlib=libstdc++; -Xlinker -stdlib=libstdc++")
find_package( OpenCV REQUIRED )
add_executable( hw2 main.cpp Union_Find.cpp Kernals.cu )
target_link_libraries( hw2 ${OpenCV_LIBS} )
我收到了这个错误:
[ 50%] Building CXX object CMakeFiles/hw2.dir/main.cpp.o
[100%] Building CXX object CMakeFiles/hw2.dir/Union_Find.cpp.o
Linking CXX executable hw2
Undefined symbols for architecture x86_64:
"callKernal(std::__1::vector<int, std::__1::allocator<int> >&, unsigned char*, int, int)", referenced from:
Union_Find::Union_Find(cv::Mat&) in Union_Find.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [hw2] Error 1
make[1]: *** [CMakeFiles/hw2.dir/all] Error 2
make: *** [all] Error 2
pkg-config opencv --cflags --libs导致:
-I/opt/local/include/opencv -I/opt/local/include -L/opt/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
答案 0 :(得分:4)
我要感谢Iwillnotexist Idonotexist对此的巨大帮助。我们致力于在Mac OSX上将所有内容链接在一起。对于Makefile,我必须取出-stdlib = libstdc ++标志并添加链接器标志:-L / usr / local / cuda / lib -lcuda -lcudart(与cmake文件类似)。可以在下面找到Makefile和CMakeLists.txt文件的工作示例。
生成文件:
CUDA_FLAGS=-L/usr/local/cuda/lib -lcuda -lcudart
LINK_FILES=`pkg-config opencv --cflags --libs`
CXX_FLAGS=-std=c++11 -std=c++0x -O3 -I/opt/local/include/opencv -I/opt/local/include
all: HW2
HW2: main.o Union_Find.o Kernals.o
g++ -o hw2 main.o Union_Find.o Kernals.o ${LINK_FILES} ${CUDA_FLAGS}
main.o: main.cpp
g++ -c main.cpp ${CXX_FLAGS}
Union_Find.o: Union_Find.hpp Union_Find.cpp
g++ -c Union_Find.cpp ${CXX_FLAGS}
Kernals.o: Kernals.hpp Kernals.cu
nvcc -c Kernals.cu
clean:
rm *.o hw2
的CMakeLists.txt
project( hw2 )
cmake_minimum_required(VERSION 2.8)
find_package(CUDA QUIET REQUIRED)
find_package( OpenCV REQUIRED )
add_definitions(-DHAVE_ALLOCA -DHAVE_ALLOCA_H -DHAVE_LIBPTHREAD -DHAVE_UNISTD_H)
SET(CUDA_HOST_COMPILER "/usr/bin/clang")
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/cuda/lib -lcuda -lcudart" )
cuda_compile(Kernel Kernals.cu)
add_executable( hw2 main.cpp Union_Find.cpp ${Kernel} )
target_link_libraries( hw2 ${OpenCV_LIBS} ${CUDA_LIBS} )