Cmake 错误:x86_64 的未定义符号

3

我正在尝试在我的Mac OSX Yosemite上使用Xcode编译卷积神经网络库。我甚至将标志设置为libstdc ++,但仍然无法正确链接。

我仍然会收到x86_64架构的未定义符号错误。 非常感谢任何帮助。

sh-3.2# ./compile.sh
Building conv-net library
-- The C compiler identification is AppleClang 6.0.0.6000056
-- The CXX compiler identification is AppleClang 6.0.0.6000056
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
WARNING: Target "testimg" requests linking to directory "/usr/local/Cellar/opencv/2.4.9/lib".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "testimg" requests linking to directory "/usr/local/Cellar/opencv/2.4.9/lib".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "testmnist" requests linking to directory "/usr/local/Cellar/opencv/2.4.9/lib".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "testmnist" requests linking to directory "/usr/local/Cellar/opencv/2.4.9/lib".  Targets may link only to libraries.  CMake is dropping the item.
-- Generating done
-- Build files have been written to: /var/tmp/conv-net-0.1-prealpha_buildroot
Scanning dependencies of target cvconvnet
[ 11%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvsubsamplingplane.cpp.o
[ 22%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvconvolutionplane.cpp.o
[ 33%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvgenericplane.cpp.o
[ 44%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvsourceplane.cpp.o
[ 55%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvrbfplane.cpp.o
[ 66%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvmaxplane.cpp.o
[ 77%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvconvnetparser.cpp.o
[ 88%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvfastsigmoid.cpp.o
[100%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvconvnet.cpp.o
/Users/prabhubalakrishnan/Desktop/conv-net/src/cvconvnet.cpp:169:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
Linking CXX static library libcvconvnet.a
[100%] Built target cvconvnet
Scanning dependencies of target testimg
Linking CXX executable testimg
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
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]: *** [testimg] Error 1
make[1]: *** [CMakeFiles/testimg.dir/all] Error 2
make: *** [all] Error 2
cp: testimg: No such file or directory
cp: testmnist: No such file or directory
ls: illegal option -- I
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

这是我的 Makefile 文件。
cmake_minimum_required(VERSION 3.0)
PROJECT (CvConvolutionalNet)
set (CMAKE_CXX_FLAGS " -stdlib=libstdc++")
# IF() ENDIF() statements
SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

# Specify build-type as Debug if not specified already
IF (NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE Debug)
ENDIF ()

# Produce verbose makefiles
# SET(CMAKE_VERBOSE_MAKEFILE ON)

# Sources for library
SET(CVCONVNET_SRCS
    src/cvsubsamplingplane.cpp
    src/cvconvolutionplane.cpp
    src/cvgenericplane.cpp
    src/cvsourceplane.cpp
    src/cvrbfplane.cpp
    src/cvmaxplane.cpp
    src/cvconvnetparser.cpp
    src/cvfastsigmoid.cpp
    src/cvconvnet.cpp
)

# Sources for test files
SET(TESTIMG_SRCS tst/)
SET(TESTMNIST_SRCS tst/)

set (CV_H /usr/local/Cellar/opencv/2.4.9/include/opencv)
set (HIGHGUI_H src/include )
set (LIBCV /usr/local/Cellar/opencv/2.4.9/lib )
set (LIBHIGHGUI /usr/local/Cellar/opencv/2.4.9/lib )

# Here are common paths (in addition to default paths)
SET (INCLUDE_SEARCH_PATH
    /usr/local/include /usr/include /usr/include/opencv/
    /usr/include/opencv/ c:/program\ files/opencv/include
)

SET (LIBRARY_SEARCH_PATH
    /usr/local/lib /usr/lib c:/program\ files/opencv/lib c:/windows/system32
)

# Find OpenCV and Expat
FIND_PATH(CV_H NAMES cv.h PATHS ${INCLUDE_SEARCH_PATH} )
FIND_PATH(HIGHGUI_H NAMES highgui.h PATHS ${INCLUDE_SEARCH_PATH} )
FIND_PATH(EXPAT_H NAMES expat.h PATHS ${INCLUDE_SEARCH_PATH} )

FIND_LIBRARY(LIBCV NAMES cv PATHS ${LIBRARY_SEARCH_PATH} )
FIND_LIBRARY(LIBHIGHGUI NAMES highgui PATHS ${LIBRARY_SEARCH_PATH} )
FIND_LIBRARY(LIBEXPAT NAMES expat PATHS ${LIBRARY_SEARCH_PATH} )

INCLUDE_DIRECTORIES(include/ ${CV_H} ${HIGHGUI_H} ${EXPAT_H})

# Here is out library
ADD_LIBRARY(cvconvnet STATIC ${CVCONVNET_SRCS})

# Here are our test programs
ADD_EXECUTABLE(testimg ${TESTIMG_SRCS})
ADD_EXECUTABLE(testmnist ${TESTMNIST_SRCS})

# Compiler options are different for Release and Debug
IF (CMAKE_BUILD_TYPE MATCHES Release)
    # Highly optimized + cancel all assert()s
    ADD_DEFINITIONS(-O3 -DNDEBUG) 
ELSE ()
    # Include debug info, profiling info, some text output
    ADD_DEFINITIONS(-O -pg -g -DDEBUG)
    # Set profiling for linker too
    SET_TARGET_PROPERTIES(testmnist PROPERTIES LINK_FLAGS "-pg")
ENDIF ()

# We should link our test programs to libraries
TARGET_LINK_LIBRARIES(testimg cvconvnet ${LIBCV} ${LIBHIGHGUI} ${LIBEXPAT})
TARGET_LINK_LIBRARIES(testmnist cvconvnet ${LIBCV} ${LIBHIGHGUI} ${LIBEXPAT})
2个回答

10

我通过在编译器标志中添加-c选项来解决了这个问题,现在它可以工作了 !!!!


4
为了澄清,要添加-c选项,请使用:set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -c") - Hustlion
-c 是什么意思? - triple
使用 -c 选项可以跳过链接步骤,只进行预处理和编译。 - skalarproduktraum

5
add_executable命令需要一个目标名称和一个要编译的源文件列表。当您为testimg创建一个目标时,您正在将目录传递给它。
使用file(GLOB ...)命令将目录中的所有源文件收集到一个列表变量中,并将其传递给add_executable调用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接