Giter Site home page Giter Site logo

cmaketutorial's Introduction

Cmake中文实战教程

本项目以代码讲用法,旨在帮助初学者学习CMake的基本用法,也会结合实际开发经验将最核心最常用的用法使用实际的代码案例进行讲解。该项目很难涵盖CMake的所有用法,本项目只会扮演一个引路者的角色,在实际的项目开发中,要学会根据需求灵活查阅官方文档解决问题。(本教程的所有章节使用c++以及linux环境演示)

目录

Github Pages

https://brightxiaohan.github.io/CMakeTutorial/

知乎博客地址

cmake 实用指南

cmaketutorial's People

Contributors

brightxiaohan avatar syaojun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cmaketutorial's Issues

求助大佬

你好我是刚学习cuda,我有一个简单的cuda代码:main.cu,cmakelist不知道怎么写才能跑通代码。麻烦指导一下。
我下面给出代码和cmakelists
(1)代码:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <memory.h>
#include <time.h>
#include "helper_timer.h"

global void increment_kernel(int g_data, int inc_value)
{
int idx = blockIdx.x
blockDim.x + threadIdx.x;
g_data[idx] = g_data[idx] + inc_value;

}

int correct_output(int data, const int n, const int x)
{
for (int i = 0; i < n; i++)
if (data[i] != x)
return 0;
}
int main(int argc, char argv[])
{
cudaDeviceProp deviceProps;
cudaGetDeviceProperties(&deviceProps, 0);
printf("CUDA device [%s]\n", deviceProps.name);
int n = 16 * 1024 * 1024;
int nbytes = n
sizeof(int);
int value = 26;
int a = 0;
cudaMallocHost((void
)&a, nbytes);
memset(a, 0, nbytes);
int d_a = 0;
cudaMalloc((void
*)&d_a, nbytes);
cudaMemset(d_a, 255, nbytes);
dim3 threads = dim3(512, 1);
dim3 blocks = dim3(n / threads.x, 1);

cudaEvent_t startGPU, stopGPU;
cudaEventCreate(&startGPU);
cudaEventCreate(&stopGPU);

StopWatchInterface *timer = NULL;
sdkCreateTimer(&timer);
sdkResetTimer(&timer);

cudaThreadSynchronize();

float gpu_time = 0.0f;


sdkStartTimer(&timer);
cudaEventRecord(startGPU, 0);
cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, 0);
increment_kernel<<<blocks, threads, 0, 0>>>(d_a, value);
cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, 0);
cudaEventRecord(stopGPU, 0);
sdkStopTimer(&timer);

unsigned long int counter = 0;

while (cudaEventQuery(stopGPU) == cudaErrorNotReady)
{
    counter++;
}

cudaEventElapsedTime(&gpu_time, startGPU, stopGPU);
printf("time spent executing by the GPU:%.2f\n", gpu_time);
printf("time spent by CPU in CUDA calls:%.8f\n", sdkGetTimerValue(&timer));
printf("GPU execute %d iteration while waiting forGPU to finish\n", counter);

printf("-------------------------------------------------------------------------\n");
if (correct_output(a, n, value))
    printf("TEST PASSED\n");
else
    printf("Test FAILED\n");

cudaEventDestroy(startGPU);
cudaEventDestroy(stopGPU);
cudaFreeHost(a);
cudaFree(d_a);
getchar();
cudaThreadExit();
return 0;

}

cmakelists:

cmake_minimum_required(VERSION 3.9)
PROJECT(testCUDA LANGUAGES CXX CUDA)

#cuda
#find_package(CUDA REQUIRED)
add_definitions(-DUSE_CUDA)
set(CUDA_INCLUDE_DIRS /usr/local/cuda/include)
set(CUDA_LIBRARY_DIRS /usr/local/cuda/lib64)

include_directories(${CUDA_INCLUDE_DIRS})
link_directories(${CUDA_LIBRARY_DIRS})

include_directories(/home/yjy/NVIDIA_CUDA-10.2_Samples/common/inc)

#add_library(testCUDA main.cu)

add_executable(testCUDA main.cu)
target_link_libraries(testCUDA ${CUDA_LIBRARY_DIRS} -lcudart_static -lstdc++ -lm -lrt -ldl -lpthread)

如何引用Installation 中的库

教程写得非常棒。在Installation
中提到了“在其他项目中可以使用

find_package(MyMath 1.0)
target_linked_library(otherapp MyMath::mymath)

我在自己的工程中,这样编写的cmake

cmake_minimum_required(VERSION 3.0)    
project(hi VERSION 0.1) 
add_executable(hi main.cpp)  
set(MyMath_DIR /opt/repos/cmake-tuts-repos/CMakeTutorial-master/Installation/build/mymath/lib/cmake/MyMath/)                               
find_package(MyMath) 
if(MyMath_FOUND) 
    message("MyMath found")  
    target_include_directories(hi PUBLIC ${MyMath_INCLUDE_DIR}) 
    message("include dir ${MyMath_INCLUDE_DIR}")   
    target_link_libraries(hi MyMath::mymath)       
else(MyMath_FOUND)     
    message("MyMath not found")   
endif(MyMath_FOUND)          

无法找到对应mymath.h 头文件,能帮我定位一下原因吗?

关于FindPackage的一个问题

请问find_package(GLOG)成功后如何用cmakelist.txt中把这个库的位置打印出来?我试着用message打印,好像只有message(${GLOG_FOUND})的值为1,你页面上说的GLOG_INCLUDE_DIR、GLOG_INCLUDES、GLOG_LIBRARY、GLOG_LIBRARIES这几个值都为空,但我想把这库的位置打印出来,请问我该怎么办呢

生成的库无法引用

我用你的方法生成了相应的模块,在另一个项目里引用了他,模块也找到了,但无法找到头文件。
在其他的库比如Qt.,libtorch等,FIND_PACKAGE以后直接target_link_libraries(就可以了,不需要target_include_directories,为什么这里就需要呢,而且加上也找不到有文件

#cmakelists.txt
project(dll_import)
SET(CMAKE_PREFIX_PATH "D:\\DESKTOP\\CODES\\CPP_CMAKE\\install\\dll_export")
add_executable(dll_import
		main.cpp)
FIND_PACKAGE(MyMath 1.0)
#target_include_directories(dll_import PUBLIC  ${MyMath_INCLUDE_DIR})
target_link_libraries(dll_import MyMath::dll_export)
//main.cpp
#include <iostream>
#include <dll_export.h>
using namespace std;
int main()
{
	double s = get_n_of_hole(1, 1.2);
	double d = get_Ui0(1.0, 3.2);
	cout << s << "---" << d << endl;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.