Undefined reference to `px4_task_kill'

Hi everybody

I am new for px4 project, and I try to compile a demo. I get a error of undefined reference to `px4_task_kill’, but in fact I have included the all the “.h“ file .

here is the demo code.

#include <px4_platform_common/defines.h>
#include <px4_platform_common/module.h>
#include <px4_platform_common/module_params.h>
#include <px4_platform_common/posix.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <px4_platform_common/sem.hpp>
#include <px4_platform_common/tasks.h>
#include <px4_platform_common/log.h>

// #include <px4_log.h>

#include <string.h>

#include <drivers/drv_hrt.h>
#include <lib/perf/perf_counter.h>

using namespace time_literals;
#include <px4_platform_common/px4_config.h>

extern “C” __EXPORT int test_app_main(int argc, char *argv);

static px4_task_t task_id = -1;
static bool _task_should_exit = false;

static const unsigned int MAX_CNT = 100000;
static unsigned int cnt = 0;
static px4_sem_t sem;
px4_task_t task_id2,task_id3;

bool is_running(void)
{
return task_id != -1;
}

int run2(int argc, char *argv)
{
for (unsigned int i = 0; i < MAX_CNT; ++i) {
px4_sem_wait(&sem);
unsigned int local_cnt = cnt;
++local_cnt;
cnt = local_cnt;
px4_sem_post(&sem);
px4_sleep(0);
}

return PX4_OK;

}

bool is_task_running(px4_task_t current_task_id) {
int ret = px4_task_kill(current_task_id, 0);
if (ret == 0) {
PX4_INFO(“Task %d is running”, current_task_id);
return true;
} else {
PX4_INFO(“Task %d not running (errno: %d)”, current_task_id, errno);
return false;
}
}

int run(void)
{

px4_sem_init(&sem, 0, 1);

task_id2 = px4_task_spawn_cmd("test_app2", SCHED_DEFAULT, SCHED_PRIORITY_DEFAULT + 10, 1024, (px4_main_t)&run2, nullptr);
	if (task_id2 < 0) {
    	PX4_ERR("Task2 spawn failed");
    	return -1;
}

task_id3 = px4_task_spawn_cmd("test_app3", SCHED_DEFAULT, SCHED_PRIORITY_DEFAULT + 10, 1024, (px4_main_t)&run2, nullptr);
if (task_id3 < 0) {
    	PX4_ERR("Task3 spawn failed");
    	return -1;
}
while (!_task_should_exit) {
	if (!is_task_running(task_id2) && !is_task_running(task_id)) {
	PX4_INFO("cnt = %d", cnt);
	}

	px4_sleep(1);
}

px4_sem_destroy(&sem);
return PX4_OK;

}

int run_trampoline(int argc, char *argv)
{
_task_should_exit = false;
run();
return PX4_OK;
}

int task_spawn(int argc, char *argv)
{
task_id = px4_task_spawn_cmd(“test_app”,
SCHED_DEFAULT,
SCHED_PRIORITY_MIN + 5,
1024,
(px4_main_t)&run_trampoline,
(char *const *)argv);

if (task_id < 0) {
	task_id = -1;
	return -1;
}

return 0;

}

int start_command_base(int argc, char *argv)
{
int ret = 0;

if (is_running()) {
	ret = -1;
	PX4_INFO("Task is already running");

} else {
	ret = task_spawn(argc, argv);

	if (ret < 0) {
		PX4_ERR("Task start failed(%i)", ret);
	}
}

return ret;

}

can someone give me some advice. thanks a lot.

That’s a linking error, not an include problem. Make sure that you have the required modules in the target_link_libraries list in CMakeLists.txt.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.