本文介绍了对“来自外部库的函数名”的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

项目(MP)
cmake_minimum_required(VERSION 2.8)

if(TYPE STREQUALDebug)
set(CMAKE_BUILD_TYPEDebug)
else()
set(CMAKE_BUILD_TYPERelease)
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 :(。文本+ 0x2d5):未定义引用`rfftw2d_create_plan'
Simulation.cc:(.text + 0x2ee):未定义引用`rfftw2d_create_plan'
CMakeFiles / SciVis.dir / Simulation.cc.o: `simulation :: solve()':
Simulation.cc:(.text+0x881):未定义引用`rfftwnd_one_real_to_complex'
Simulation.cc:(.text+0x891):undefined引用`rfftwnd_one_real_to_complex '
Simulation.cc:(.text+0xa7f):未定义引用`rfftwnd_one_complex_to_real'
Simulation.cc:(.text+0xa8f):未定义引用`rfftwnd_one_complex_to_real'
CMakeFiles / SciVis .dir / Simulation.cc.o:在函数`Simulation :: FFT(int,void *)'中:
Simulation.cc:(.text+0x390):未定义引用`rfftwnd_one_complex_to_real'
模拟.cc :(。text + 0x3a0):未定义引用`rfftwnd_one_real_to_complex'
collect2:错误:ld返回1退出状态
make [2]:*** [SciVis]错误1
make [1]:*** [CMakeFiles / SciVis.dir / all]错误2
make:*** [全部]错误2

在我的Simulation.cc文件中:

  #include< fftw.h> ; 
void Simulation :: init_simulation(size_t n)
{
//分配数据结构
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);

//初始化数据结构为0
for(size_t i = 0; i {
vx [i] = vy [i] = vy0 [i] = fx [i] = fy [i] = rho [i]
}
}

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,找到库,并链接到它首先,将这个文件在您的项目的目录下一个新的文件夹CMakeModules。



 # - 查找FFTW 
#查找原生FFTW包含和库

#FFTW_INCLUDES - 在哪里可以找到fftw3.h
#FFTW_LIBRARIES - 使用FFTW时的库列表。
#FFTW_FOUND - 如果找到FFTW,则为真。

if(FFTW_INCLUDES)
#已经在缓存中,静默
set(FFTW_FIND_QUIETLY TRUE)
endif(FFTW_INCLUDES)

find_path FFTW_INCLUDES fftw3.h)

find_library(FFTW_LIBRARIES NAMES fftw3)

#处理QUIETLY和REQUIRED参数,如果
#所有列出的变量都为TRUE,则将FFTW_FOUND设置为TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FFTW DEFAULT_MSG FFTW_LIBRARIES FFTW_INCLUDES)

mark_as_advanced(FFTW_LIBRARIES FFTW_INCLUDES)

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

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


I'm using Ubuntu 12 64 bit, installed fftw library version 2.1.5.I have a c++ project with use CMake to build the make file. This is my 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})

When I tried running the make file create by Cmake, i got this problem:CMakeFiles/SciVis.dir/Simulation.cc.o: In function `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

In my Simulation.cc file:

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

I dont know where I were wrong, can someone please help me ?
Thank you very much.

解决方案

You're not linking against FFTW, you need to make CMake find the library and link against it first, put this file in your project's directory under a new folder "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)

Next, add this line to the top of your CMakeLists.txt:

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

Now try this:

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

这篇关于对“来自外部库的函数名”的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 18:11