【问题标题】:CMake / g++, library not being linked in OpenFOAM applicationCMake / g++,库未在 OpenFOAM 应用程序中链接
【发布时间】:2013-06-17 17:04:29
【问题描述】:

我正在尝试编译自定义 OpenFOAM 应用程序。我的构建过程是使用 CMake(虽然我不确定这与我当前的问题有什么关系)。

对于熟悉 OpenFOAM 的人来说,这是 pisoFoam 应用程序,问题库是 incompressibleLESModels.so 库。

项目构建没有任何问题。并运行直到它需要使用 IncompressibleLESModels 库。此时,应用声称对该库一无所知并停止。

我在 CMake 脚本中的 TARGET_LINK_LIBRARIES 中包含了 incompressibleLESModels 库(以及所有其他必要的库)。

OpenFOAM 允许用户在运行时通过输入文件链接库。此方法工作正常(即,我可以让应用程序动态加载到 incompressibleLESModels 库中并运行)。但我宁愿不依赖这种方法。而标准的 OpenFOAM 应用程序不会这样做。

当我在我的可执行文件上运行 ldd 时,incompressibleLESModels 库显然不在库列表中。

所以就好像链接器在链接阶段检测到不需要该库并选择不链接它。据我了解,这可能是由于传递给 gcc 的定义,特别是 add-needed,或者按需和非按需。

我通过 CMake 中的 ADD_DEFINITIONS 命令添加以下定义:

-DWM_DP  -m64  -Dlinux64  -Wall -Wextra -Wno-unused-parameter -Wold-style-cast 
-Wnon-virtual-dtor -O3  -DNoRepository -ftemplate-depth-100 -fPIC -Xlinker 
--add-needed -Xlinker --no-as-needed

非常感谢任何想法。

亲切的问候,玛德琳

【问题讨论】:

    标签: c++ gcc dll linker cmake


    【解决方案1】:

    由于您使用的是 cmake,对于链接指令而不是使用 add_definitions,请使用 target_link_libraries

    target_link_libraries(<targetname> "-Wl,--no-as-needed")
    target_link_libraries(<targetname> <libraries that you want to link even if apparently not necessary>)
    target_link_libraries(<targetname> "-Wl,--add-needed")
    target_link_libraries(<targetname> <libraries that you want to link according to the "default" criteria>)
    

    参考:http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:target_link_libraries

    此外,对于 -fPIC 等,建议您使用 CMAKE_CXX_FLAGS 之类的变量,并向其附加标志。

    设置(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

    注意:使用 list 和 append 将不起作用,因为通常列表元素用分号分隔。您可以根据构建类型设置标志,例如 CMAKE_CXX_FLAGS_DEBUG 和 CMAKE_CXX_FLAGS_RELEASE。

    http://www.cmake.org/cmake/help/v2.8.11/cmake.html#variable:CMAKE_LANG_FLAGS_DEBUG http://www.cmake.org/cmake/help/v2.8.11/cmake.html#variable:CMAKE_LANG_FLAGS_RELEASE

    【讨论】:

    • 我猜你的意思是target_link_libraries,而不是target_linker_libraries
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多