对"function name from external library"的未定义引用

Undefined reference to "function name from external library"

本文关键字:未定义 引用 library from function name external      更新时间:2023-10-16

我使用的是Ubuntu 12 64位,安装了2.1.5版本的fftw库。我有一个c++项目,使用CMake来构建make文件。这是我的cmakelist.text:

project(MP)
cmake_minimum_required(VERSION 2.8)
if(TYPE STREQUAL "Debug")
        set(CMAKE_BUILD_TYPE "Debug")
else()
        set(CMAKE_BUILD_TYPE "Release")
endif()

if(CMAKE_COMPILER_IS_GNUCXX)
    add_definitions( -std=c++11 )
endif()

find_package(GLUT REQUIRED)
find_package(OpenGL REQUIRED)
find_library(GLUI libglui.a ./vendor/lib)

include_directories(${OPENGL_INCLUDE_DIR}
                   ./vendor/include)
LINK_DIRECTORIES(/usr/local/lib)
LINK_DIRECTORIES(/usr/lib)
LINK_DIRECTORIES(/usr/bin)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} GL GLU glut ${GLUI})

当我尝试运行Cmake创建的make文件时,我遇到了这个问题:CMakeFiles/SciVis.dir/Simulation.cc.o:在函数"Simulation::init_Simulation(unsigned long)"中:

Simulation.cc:(.text+0x2d5): undefined reference to `rfftw2d_create_plan'
Simulation.cc:(.text+0x2ee): undefined reference to `rfftw2d_create_plan'
CMakeFiles/SciVis.dir/Simulation.cc.o: In function `Simulation::solve()':
Simulation.cc:(.text+0x881): undefined reference to `rfftwnd_one_real_to_complex'
Simulation.cc:(.text+0x891): undefined reference to `rfftwnd_one_real_to_complex'
Simulation.cc:(.text+0xa7f): undefined reference to `rfftwnd_one_complex_to_real'
Simulation.cc:(.text+0xa8f): undefined reference to `rfftwnd_one_complex_to_real'
CMakeFiles/SciVis.dir/Simulation.cc.o: In function `Simulation::FFT(int, void*)':
Simulation.cc:(.text+0x390): undefined reference to `rfftwnd_one_complex_to_real'
Simulation.cc:(.text+0x3a0): undefined reference to `rfftwnd_one_real_to_complex'
collect2: error: ld returned 1 exit status
make[2]: *** [SciVis] Error 1
make[1]: *** [CMakeFiles/SciVis.dir/all] Error 2
make: *** [all] Error 2

在我的Simulation.cc文件中:

#include <fftw.h>
void Simulation::init_simulation(size_t n)
{
    //Allocate data structures
    size_t dim     = n * 2 * (n / 2 + 1);
    vx       = new fftw_real[dim];
    vy       = new fftw_real[dim];
    vx0      = new fftw_real[dim];
    vy0      = new fftw_real[dim];
    fx      = new fftw_real[n * n];
    fy      = new fftw_real[n * n];
    rho     = new fftw_real[n * n];
    rho0    = new fftw_real[n * n];
    plan_rc = rfftw2d_create_plan(n, n, FFTW_REAL_TO_COMPLEX, FFTW_IN_PLACE);
    plan_cr = rfftw2d_create_plan(n, n, FFTW_COMPLEX_TO_REAL, FFTW_IN_PLACE);
    // Initialize data structures to 0
    for (size_t i = 0; i < n * n; i++)
    { 
        vx[i] = vy[i] = vx0[i] = vy0[i] = fx[i] = fy[i] = rho[i] = rho0[i] = 0.0f; 
    }
}
void Simulation::FFT(int direction,void* vx)
{
    if(direction==1) rfftwnd_one_real_to_complex(plan_rc,(fftw_real*)vx,(fftw_complex*)vx);
    else             rfftwnd_one_complex_to_real(plan_cr,(fftw_complex*)vx,(fftw_real*)vx);
}

我不知道我错在哪里了,有人能帮帮我吗?
非常感谢。

您没有针对FFTW进行链接,您需要让CMake找到库并首先针对它进行链接,将此文件放在项目目录下的新文件夹"CMakeModules"下。

FindFFTW.cmake

# - Find FFTW
# Find the native FFTW includes and library
#
#  FFTW_INCLUDES    - where to find fftw3.h
#  FFTW_LIBRARIES   - List of libraries when using FFTW.
#  FFTW_FOUND       - True if FFTW found.
if (FFTW_INCLUDES)
  # Already in cache, be silent
  set (FFTW_FIND_QUIETLY TRUE)
endif (FFTW_INCLUDES)
find_path (FFTW_INCLUDES fftw3.h)
find_library (FFTW_LIBRARIES NAMES fftw3)
# handle the QUIETLY and REQUIRED arguments and set FFTW_FOUND to TRUE if
# all listed variables are TRUE
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (FFTW DEFAULT_MSG FFTW_LIBRARIES FFTW_INCLUDES)
mark_as_advanced (FFTW_LIBRARIES FFTW_INCLUDES)

接下来,将此行添加到CMakeLists.txt的顶部:

project(MP)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules" ${CMAKE_MODULE_PATH})

现在试试这个:

find_package(FFTW REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${FFTW_INCLUDES}
                   ./vendor/include)
...
target_link_libraries(${PROJECT_NAME} GL GLU glut ${GLUI} ${FFTW_LIBRARIES})