cmake连接静态库

来源:互联网 发布:网络流行歌曲在线试听 编辑:程序博客网 时间:2024/06/07 10:35

制作的静态库为hello_base,文件为hello.cc hello.h

hello.h内容

#ifndef __HELLO_H__#define __HELLO_H__class Hello{ public:      Hello(){}    virtual void Print();    virtual ~Hello(){}};  //fuck this semi which I forget and got compiler error as a newbie in cpp ;#endif
hello.cc内容:

#include<stdio.h>#include"hello.h"void Hello::Print(){    printf("hello world\n");}
对应的CMakeList.txt内容

PROJECT(project)cmake_minimum_required(VERSION 2.6)SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64 -g -Wall  -O2")SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -g -Wall -O2")# c++11 requiredset(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_STANDARD_REQUIRED ON)include_directories(.)set(SOURCE_FILEShello.cc)add_library(hello_base STATIC ${SOURCE_FILES})


生成库后将hello.h与hello_base的静态库放入主文件中供程序调用,主文件夹为test_main,中一个文件test.cc

test.cc

#include"hello.h"#include<stdio.h>class Test:public Hello{    public:    Test(){}    void Print() override    {        printf("hello fuck\n");    }   ~Test(){printf("hello destructor\n");}};int main(){    Test test;    test.Print();    return 0;}

CMkaLists.txt

PROJECT(project)cmake_minimum_required(VERSION 2.6)SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64 -g -Wall  -O2")SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -g -Wall -O2")# c++11 requiredset(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_STANDARD_REQUIRED ON)include_directories(.)LINK_DIRECTORIES(.)#tell the Makefile where to find the static libraryset(SOURCE_FILEStest.cc)set(EXECUTABLE_NAME "demo")add_executable(${EXECUTABLE_NAME} test.cc)target_link_libraries(${EXECUTABLE_NAME}  hello_base)


作为记录,以备后用

原创粉丝点击