跨平台项目中 Eigen 出错
Error with Eigen in cross-platform project
我正在开发一个动态库,它将在 Windows 和 Linux 软件上链接。为了简化构建操作我决定使用 cmake.
库具有以下结构:
MyProject
└ Subproject_1
| └ ...
| └ CMakeLists.txt
└ Subproject_2
| └ ...
| └ CMakeLists.txt
└ ...
└ CMakeLists.txt
在Subproject_1中我使用了Eigen 3.3(我下载了latest release)和OpenCV 3.3.0.
主要的 CMakeLists 文件是这个
cmake_minimum_required (VERSION 2.8)
SET(CMAKE_CXX_STANDARD 11)
project (MyProject)
## Output directory configuration
SET(out_dir ./)
SET(EXECUTABLE_OUTPUT_PATH ${out_dir}../lib/ CACHE PATH "Build directory" FORCE)
SET(LIBRARY_OUTPUT_PATH ${out_dir}../lib/ CACHE PATH "Build directory" FORCE)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${out_dir}../lib)
SET(CMAKE_BUILD_FILES_DIRECTORY ${out_dir})
SET(CMAKE_BUILD_DIRECTORY ${out_dir})
SET(CMAKE_BINARY_DIR ${out_dir})
SET(CMAKE_CACHEFILE_DIR ${out_dir})
SET(opencv_path "")
if (WIN32 OR MSVC OR MSYS OR MINGW)
SET(opencv_path "../opencv-3.3.0/build64/")
endif()
message("Setted OpenCV path to '" ${opencv_path} "'")
find_package(OpenCV REQUIRED PATHS ${opencv_path}
opencv_core opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs opencv_imgproc opencv_photo opencv_plot opencv_xfeatures2d opencv_ml opencv_video
)
# Additional dependencies
include_directories(
${OPENCV_INCLUDE_DIRS}
"../eigen/"
"Subprojects_1/include"
"Subprojects/include"
)
## Subprojects
add_subdirectory ("Subproject_1")
add_subdirectory ("Subproject_2")
而 Subproject_1 CMakeLists 文件是
cmake_minimum_required (VERSION 2.8)
project(Subproject_1)
file (GLOB core_file "include/*.h" "src/*.cpp")
add_library(library SHARED ${core_file})
target_link_libraries(library
${OpenCV_LIBRARIES}
)
现在,当我在 Windows 上编译项目时一切正常,但如果我尝试在 Ubuntu 16.04 LTS 上编译它,我会遇到以下错误:
... error: cannot declare reference to ‘Eigen::MatrixXi& {aka class Eigen::Matrix<int, -1, -1>&}’, which is not a typedef or a template type argument
static void bitwiseAND(const Eigen::MatrixXi& m1, const Eigen::MatrixXi& m2, Eigen::MatrixXi& and);
^
... error: cannot declare reference to ‘Eigen::MatrixXi& {aka class Eigen::Matrix<int, -1, -1>&}’, which is not a typedef or a template type argument
static void bitwiseNOT(const Eigen::MatrixXi& m, Eigen::MatrixXi& not);
^
我很确定问题不是由 cmake 引起的,但我在 Google 或这里没有找到任何可以帮助我解决问题的内容。此外,我尝试遵循 Eigen official instructions 但它们在 Windows.
上对我不起作用
准确地说,我正在使用:
- 于 Windows:Visual Studio 2017,制作版本 3.8 和 Ninja
- 在 Ubuntu 上:制作版本 4.1
库和 Eigen 文件在两个操作系统上是相同的,因为我共享包含两个存储库的工作区目录,而且我已经尝试在 Ubuntu.
欢迎大家提出建议
您必须避免在 Linux 下使用 not
和 and
作为变量名,您应该将它们重命名为其他名称以便您的代码编译。
它们实际上是 C++ 关键字(see here), which causes problems under Linux. Visual Studio does not recognize them, which explains why the compilation works under Windows. See this SO question 了解详情。
我正在开发一个动态库,它将在 Windows 和 Linux 软件上链接。为了简化构建操作我决定使用 cmake.
库具有以下结构:
MyProject
└ Subproject_1
| └ ...
| └ CMakeLists.txt
└ Subproject_2
| └ ...
| └ CMakeLists.txt
└ ...
└ CMakeLists.txt
在Subproject_1中我使用了Eigen 3.3(我下载了latest release)和OpenCV 3.3.0.
主要的 CMakeLists 文件是这个
cmake_minimum_required (VERSION 2.8)
SET(CMAKE_CXX_STANDARD 11)
project (MyProject)
## Output directory configuration
SET(out_dir ./)
SET(EXECUTABLE_OUTPUT_PATH ${out_dir}../lib/ CACHE PATH "Build directory" FORCE)
SET(LIBRARY_OUTPUT_PATH ${out_dir}../lib/ CACHE PATH "Build directory" FORCE)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${out_dir}../lib)
SET(CMAKE_BUILD_FILES_DIRECTORY ${out_dir})
SET(CMAKE_BUILD_DIRECTORY ${out_dir})
SET(CMAKE_BINARY_DIR ${out_dir})
SET(CMAKE_CACHEFILE_DIR ${out_dir})
SET(opencv_path "")
if (WIN32 OR MSVC OR MSYS OR MINGW)
SET(opencv_path "../opencv-3.3.0/build64/")
endif()
message("Setted OpenCV path to '" ${opencv_path} "'")
find_package(OpenCV REQUIRED PATHS ${opencv_path}
opencv_core opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs opencv_imgproc opencv_photo opencv_plot opencv_xfeatures2d opencv_ml opencv_video
)
# Additional dependencies
include_directories(
${OPENCV_INCLUDE_DIRS}
"../eigen/"
"Subprojects_1/include"
"Subprojects/include"
)
## Subprojects
add_subdirectory ("Subproject_1")
add_subdirectory ("Subproject_2")
而 Subproject_1 CMakeLists 文件是
cmake_minimum_required (VERSION 2.8)
project(Subproject_1)
file (GLOB core_file "include/*.h" "src/*.cpp")
add_library(library SHARED ${core_file})
target_link_libraries(library
${OpenCV_LIBRARIES}
)
现在,当我在 Windows 上编译项目时一切正常,但如果我尝试在 Ubuntu 16.04 LTS 上编译它,我会遇到以下错误:
... error: cannot declare reference to ‘Eigen::MatrixXi& {aka class Eigen::Matrix<int, -1, -1>&}’, which is not a typedef or a template type argument
static void bitwiseAND(const Eigen::MatrixXi& m1, const Eigen::MatrixXi& m2, Eigen::MatrixXi& and);
^
... error: cannot declare reference to ‘Eigen::MatrixXi& {aka class Eigen::Matrix<int, -1, -1>&}’, which is not a typedef or a template type argument
static void bitwiseNOT(const Eigen::MatrixXi& m, Eigen::MatrixXi& not);
^
我很确定问题不是由 cmake 引起的,但我在 Google 或这里没有找到任何可以帮助我解决问题的内容。此外,我尝试遵循 Eigen official instructions 但它们在 Windows.
上对我不起作用准确地说,我正在使用:
- 于 Windows:Visual Studio 2017,制作版本 3.8 和 Ninja
- 在 Ubuntu 上:制作版本 4.1
库和 Eigen 文件在两个操作系统上是相同的,因为我共享包含两个存储库的工作区目录,而且我已经尝试在 Ubuntu.
欢迎大家提出建议
您必须避免在 Linux 下使用 not
和 and
作为变量名,您应该将它们重命名为其他名称以便您的代码编译。
它们实际上是 C++ 关键字(see here), which causes problems under Linux. Visual Studio does not recognize them, which explains why the compilation works under Windows. See this SO question 了解详情。