尊龙凯时人生就是搏

ʹÓÃ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, &params);

µÇ¼ºó¸´ÖÆ

Èý¡¢ÉèÖÃʵʱʱÖÓ

ΪÁË»ñµÃ¸ü¸ßµÄʱ¼äÇø·ÖÂʺÍ׼ȷÐÔ £¬ÎÒÃÇ¿ÉÒÔʹÓûùÓÚÕæʵʱ¼äµÄʱÖÓ¡£ÔÚ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ʵʱӦÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ