目录切到build中,然后cmake ..,生成VS解决方案
chengChu 文件夹
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) #windows构建动态库时 , 加上这个会生成dll文件的同时生成lib文件,否则只会生成dll文件
set(srcs mul.cpp div.cpp )
add_library( ${LIBCHENGCHU} SHARED ${srcs})
int chufa(int a,int b){
return a/b;
}
int chengfa(int a,int b){
return a*b;
}
jiaJian文件夹
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set(srcs add.cpp sub.cpp )
add_library( ${LIBJIAJIAN} STATIC ${srcs})
int jiafa(int a,int b){
return a+b;
}
int jianfa(int a,int b){
return a-b;
}
include文件夹
int jiafa(int a,int b);
int jianfa(int a,int b);
int chengfa(int a,int b);
int chufa(int a,int b);
main文件夹
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
link_directories(${PROJECT_SOURCE_DIR}/lib)
add_executable(mainPro main.cpp )
target_link_libraries(mainPro ${LIBJIAJIAN})
target_link_libraries(mainPro ${LIBCHENGCHU})
#include <iostream>
using namespace std;
#include "headers.h"
int main(){
cout<<jiafa(8,9)<<endl;
cout<<jianfa(8,9)<<endl;
cout<<chengfa(8,9)<<endl;
cout<<chufa(18,16)<<endl;
getchar();
return 0;
}
FR:徐海涛(hunkxu)