ʹÓÃCMake¹¹½¨LinuxʵʱӦÓóÌÐòµÄÉèÖü¼ÇÉ
ʹÓÃcmake¹¹½¨ linux ʵʱӦÓóÌÐòµÄÉèÖü¼ÇÉ
СÐò£º
Ëæ×ÅʵʱÅÌËãÐèÇóµÄÔöÌí£¬¹¹½¨ÊµÊ±Ó¦ÓóÌÐòµÄÐèÇóÒ²Ô½À´Ô½¶à¡£CMakeÊÇÒ»ÖÖÊ¢ÐеĿçƽ̨¹¹½¨¹¤¾ß£¬ËüÌṩÁ˸»ºñµÄÉèÖÃÑ¡ÏîºÍÒ×ÓÚʹÓõÄÓï·¨£¬¿ÉÒÔ×ÊÖú¿ª·¢Õ߸ßЧµØ¹¹½¨¾ßÓÐʵʱÐÔµÄÓ¦ÓóÌÐò¡£±¾ÎĽ«ÏÈÈÝһЩʹÓÃCMake¹¹½¨LinuxʵʱӦÓóÌÐòµÄÉèÖü¼ÇÉ£¬²¢ÌṩÏà¹Ø´úÂëʾÀý¡£
Ò»¡¢ÉèÖÃCMakeLists.txtÎļþ
ÔÚ×îÏȹ¹½¨LinuxʵʱӦÓóÌÐò֮ǰ£¬ÎÒÃÇÐèÒªÏÈÉèÖÃCMakeLists.txtÎļþ¡£CMakeLists.txtÊÇCMakeµÄ¹¹½¨¾ç±¾£¬ÓÃÓÚÖ¸¶¨ÏîÄ¿µÄ¹¹½¨ÐÅÏ¢ºÍÒÀÀµÏî¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚCMakeLists.txtÎļþÖÐÌí¼ÓÒÔÏÂÐУ¬ÒÔÆôÓÃC99±ê×¼ºÍʹÓÃRT£¨Real-Time£©¿â£º
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) find_library(RT_LIBRARY rt)
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔÖ¸¶¨ÏîÄ¿µÄÃû³Æ¡¢Ô´ÎļþºÍÄ¿µÄ¿ÉÖ´ÐÐÎļþ£º
cmake_minimum_required(VERSION 3.12) project(realtime_app) set(SOURCE_FILES main.c) add_executable(realtime_app ${SOURCE_FILES})
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÖ¸¶¨ÊµÊ±Ó¦ÓóÌÐòËùÐèµÄÆäËûÒÀÀµÏî¡£ÀýÈ磬ÈôÊÇÎÒÃÇÐèҪʹÓÃPOSIXÏ߳̿⣬Ôò¿ÉÒÔÌí¼ÓÒÔÏÂÐУº
find_package(Threads REQUIRED)
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔ½«ÆäÌí¼Óµ½Ä¿µÄ¿ÉÖ´ÐÐÎļþÖУº
target_link_libraries(realtime_app ${RT_LIBRARY} Threads::Threads)
µÇ¼ºó¸´ÖÆ
¶þ¡¢ÉèÖÃʵʱµ÷ÀíÕ½ÂÔ
ÔÚLinuxÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃsched_setschedulerº¯ÊýÀ´Ñ¡Ôñʵʱµ÷ÀíÕ½ÂÔ¡£ÔÚCMakeLists.txtÎļþÖУ¬ÎÒÃÇ¿ÉÒÔÌí¼ÓÒÔÏ´úÂëÀ´ÉèÖÃʵʱµ÷ÀíÕ½ÂÔ£º
target_link_libraries(realtime_app ${RT_LIBRARY} Threads::Threads) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE") target_compile_definitions(realtime_app PRIVATE _GNU_SOURCE)
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔÔÚ´úÂëÖÐʹÓÃsched_setschedulerº¯ÊýÀ´ÉèÖÃʵʱµ÷ÀíÕ½ÂÔ£º
#include <sched.h> struct sched_param params; params.sched_priority = sched_get_priority_max(SCHED_FIFO); sched_setscheduler(0, SCHED_FIFO, ¶ms);
µÇ¼ºó¸´ÖÆ
Èý¡¢ÉèÖÃʵʱʱÖÓ
ΪÁË»ñµÃ¸ü¸ßµÄʱ¼äÇø·ÖÂʺÍ׼ȷÐÔ£¬ÎÒÃÇ¿ÉÒÔʹÓûùÓÚÕæʵʱ¼äµÄʱÖÓ¡£ÔÚCMakeLists.txtÎļþÖУ¬ÎÒÃÇ¿ÉÒÔÌí¼ÓÒÔÏ´úÂëÀ´ÉèÖÃʵʱʱÖÓ£º
target_link_libraries(realtime_app ${RT_LIBRARY} Threads::Threads) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE") target_compile_definitions(realtime_app PRIVATE _GNU_SOURCE) find_library(REALTIME_LIBRARY rt)
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔ½«ÊµÊ±Ê±ÖÓ¿âÌí¼Óµ½Ä¿µÄ¿ÉÖ´ÐÐÎļþÖУº
target_link_libraries(realtime_app ${RT_LIBRARY} ${REALTIME_LIBRARY} Threads::Threads)
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔÔÚ´úÂëÖÐʹÓÃclock_nanosleepº¯ÊýÀ´Ê¹ÓÃʵʱʱÖÓ£º
#include <time.h> struct timespec sleep_time; sleep_time.tv_sec = 0; sleep_time.tv_nsec = 1000000; // 1 millisecond clock_nanosleep(CLOCK_REALTIME, 0, &sleep_time, NULL);
µÇ¼ºó¸´ÖÆ
½áÂÛ£º
±¾ÎÄÏÈÈÝÁËʹÓÃcmake¹¹½¨ linux ʵʱӦÓóÌÐòµÄÉèÖü¼ÇÉ£¬²¢ÌṩÁËÏà¹Ø´úÂëʾÀý¡£Í¨¹ýÉèÖÃCMakeLists.txtÎļþ£¬Ñ¡Ôñʵʱµ÷ÀíÕ½ÂÔºÍÉèÖÃʵʱʱÖÓ£¬ÎÒÃÇ¿ÉÒÔΪʵʱӦÓóÌÐòÌṩ¸üºÃµÄÐÔÄܺÍ׼ȷÐÔ¡£Ï£Íû±¾ÎÄÄÜ×ÊÖú¿ª·¢Õ߸üºÃµØ¹¹½¨ÊµÊ±Ó¦ÓóÌÐò£¬²¢ÊµÏÖʵʱÅÌËãÐèÇóµÄ¸ßЧ´¦Àí¡£
²Î¿¼ÎÄÏ×£º
“CMake 3.12 Documentation” [Online]. Available: https://cmake.org/documentation/
“POSIX Threads Programming” [Online]. Available: http://man7.org/linux/man-pages/man7/pthreads.7.html
“Linux Scheduler” [Online]. Available: http://man7.org/linux/man-pages/man7/sched.7.html
“Real-time Clocks” [Online]. Available: http://man7.org/linux/man-pages/man7/clock_gettime.7.html
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨LinuxʵʱӦÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡