使用 CMake 设置 SystemC 项目:对 `sc_core 的未定义引用

Setting up a SystemC project with CMake: undefined reference to `sc_core

我正在尝试使用 CMake 在 SystemC 中构建一个简单的 hello world。

这是SystemC文件main.cpp:

#include <systemc.h>

using namespace std;

SC_MODULE (hello_world) {
  SC_CTOR (hello_world) {
  }

  void say_hello() {
    cout << "Hello World SystemC" << endl;
  }
};

int sc_main(int argc, char* argv[]) {
  hello_world hello("HELLO");
  hello.say_hello();
  return(0);
}

这里是 CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)
project(SystemCExample)

set (CMAKE_PREFIX_PATH /usr/local/systemc-2.3.2)

include_directories(${CMAKE_PREFIX_PATH}/include)

find_library(systemc systemc ${CMAKE_PREFIX_PATH}/lib-linux64)
link_directories(${CMAKE_PREFIX_PATH}/lib-linux64)

set(CMAKE_CXX_STANDARD 11) # C++11...

set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...

set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11

aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

target_link_libraries(SystemCExample systemc)

我一直收到错误消息:

/usr/local/systemc-2.3.2/include/sysc/kernel/sc_ver.h:179: error: undefined reference to `sc_core::sc_api_version_2_3_2_cxx201103L<&sc_core::SC_DISABLE_VIRTUAL_BIND_UNDEFINED_>::sc_api_version_2_3_2_cxx201103L(sc_core::sc_writer_policy)'

它指向sc_ver.h行:

api_version_check
(
  SC_DEFAULT_WRITER_POLICY
);

当我将 main.cpp 替换为另一个简单示例时,也会出现错误消息。我该如何解决?

您很可能已经使用 C++98 构建了 SystemC。这是默认的。目前,它要求您在库构建期间和您的应用程序中使用相同的 C++ 标准。

以下是使用 CMake 构建 SystemC 2.3.2 的步骤

  1. 下载SystemC 2.3.2,解压,将目录切换到systemc-2.3.2

    cd /path/to/systemc-2.3.2

  2. 创建构建目录:

    mkdir build

  3. 配置支持 C++11 的 SystemC 构建。我还建议在调试模式下构建它,这有助于学习。稍后您可以切换到发布版本以加速模拟

    cmake ../ -DCMAKE_CXX_STANDARD=11 -DCMAKE_BUILD_TYPE=Debug

  4. 构建

    cmake --build .

CMake 会自动将 SystemC 库目标导出到用户包注册表:https://cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html#user-package-registry

您可以选择将其安装在某个地方,阅读 CMake 手册以了解如何操作。

现在尝试创建示例 SystemC 项目:

main.cpp

#include <systemc.h>

SC_MODULE (hello_world) {
    SC_CTOR (hello_world)
    {
        SC_THREAD(say_hello);
    }

    void say_hello()
    {
        cout << "Hello World SystemC" << endl;
    }

};

int sc_main(int argc, char* argv[])
{
    hello_world hello("HELLO");
    sc_start();

    return (0);
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(test_systemc)

find_package(SystemCLanguage CONFIG REQUIRED)
set (CMAKE_CXX_STANDARD ${SystemC_CXX_STANDARD})

add_executable(test_systemc main.cpp)
target_link_libraries(test_systemc SystemC::systemc)

构建,运行,预期输出:

./test_systemc

        SystemC 2.3.2 --- Oct 14 2017 19:38:30
        Copyright (c) 1996-2017 by all Contributors,
        ALL RIGHTS RESERVED
Hello World SystemC