【问题标题】:I cannot get to link a shared library with another using cmake我无法使用 cmake 将共享库与另一个链接
【发布时间】:2014-10-24 10:20:27
【问题描述】:

我是 CMake 的新手,我正在尝试用它做一些实验。
在这里,我试图将共享库 renderHeader 链接到另一个共享库 test
我有以下代码示例:

test.cpp

#include <stdio.h>
#include "test.hpp"
#include "renderHeader.hpp"
using namespace std;

void sayHello(char* name) {
    __renderHeader();
    printf("Hello dear %s, Good evening.", name);
}

renderHeader.cpp

#include "renderHeader.hpp"
#include <stdio.h>
using namespace std;

void __rendeHeader() {
    printf("This is another shared lib\n");
}

main.cpp

#include <stdlib.h>
#include "test.hpp"

int main(){
    sayHello("dariush");
    exit(EXIT_SUCCESS);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(SHARED_LIB_TEST)

add_library(test SHARED test.cpp)

add_library(renderheader SHARED renderHeader.cpp)

add_executable(myapp main.cpp)

SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)

SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)

LINK_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lib)

TARGET_LINK_LIBRARIES(test renderheader)

TARGET_LINK_LIBRARIES(myapp test)

当我尝试使用CMake 编译时,它给了我__renderHeader() 未在sayHello() 中定义的错误,命令的输出如下:

CMake

$ cmake .. && make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dariush/Desktop/sharedtest/cmake
Linking CXX shared library ../lib/librenderheader.so
[ 33%] Built target renderheader
[ 66%] Building CXX object CMakeFiles/test.dir/test.cpp.o
/home/dariush/Desktop/sharedtest/test.cpp: In function ‘void sayHello(char*)’:
/home/dariush/Desktop/sharedtest/test.cpp:7:20: error: ‘__renderHeader’ was not declared in this scope
     __renderHeader();
                    ^
make[2]: *** [CMakeFiles/test.dir/test.cpp.o] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

我的问题:我做错了什么?

【问题讨论】:

  • 抱怨的不是 CMake,而是你的编译器... CMake != compiler
  • @LightnessRacesinOrbit 因为这是我第一次与 cmake 链接,它给了我错误,所以我想也许我的 CMakeLists.txt 脚本与它有关,我没有考虑其他事情“兄弟”...

标签: c++ compilation cmake shared-libraries


【解决方案1】:

很确定

void __rendeHeader() {

应该是 void __renderHeader() {

【讨论】:

  • 你建议的有什么区别??
  • 糟糕!我是哑巴,拼错了!! :)
猜你喜欢
  • 1970-01-01
  • 2013-10-25
  • 2013-10-09
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
相关资源
最近更新 更多