未定义的引用“功能名来自外部库”(Undefined reference to “function

2019-10-19 01:31发布

我使用Ubuntu 12的64位,安装FFTW库版本2.1.5。 我有使用CMake的一个C ++项目,建立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})

当我试图运行make文件由CMake的创建,我得到这个问题:CMakeFiles / SciVis.dir / Simulation.cc.o:在功能`仿真:: init_simulation(无符号长)“:

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);
}

我不知道我错了,有人可以帮我吗?
非常感谢你。

Answer 1:

你不链接到的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})


文章来源: Undefined reference to “function name from external library”
标签: c++ cmake fftw