C语言 为什么目标文件的.text部分是空的?

wlp8pajw  于 2023-01-08  发布在  其他
关注(0)|答案(1)|浏览(130)

问题

我正在使用catkin_make编译下面的代码。我有一个c文件schedule_wrappers.c和两个c++文件deadline_thread.cppstepper_motor.cpp,它们被用来在cmake中创建一个可执行文件,代码如下:
add_executable(stepper_motor src/stepper_motor.cpp src/deadline_thread.cpp src/schedule_wrappers.c)
stepper_motor.cpp文件生成了一个"合理的" .o文件(objdump生成了我所期望的输出),但是当cmake到达链接步骤时,它说:

/usr/bin/ld: CMakeFiles/stepper_motor.dir/src/stepper_motor.cpp.o: in function `StepperMotorTB6600::run()':
/home/spovilus/catkin_ws/src/beginner_tutorials/include/stepper_motor_TB6600.hpp:73: undefined reference to `wrapper_sched_yield'
/usr/bin/ld: CMakeFiles/stepper_motor.dir/src/stepper_motor.cpp.o: in function `Motor::Motor(int, int)':
/home/spovilus/catkin_ws/src/beginner_tutorials/include/motor.hpp:43: undefined reference to `DeadlineThread::DeadlineThread(int, int)'
/usr/bin/ld: CMakeFiles/stepper_motor.dir/src/stepper_motor.cpp.o:(.data.rel.ro._ZTI5Motor[_ZTI5Motor]+0x18): undefined reference to `typeinfo for DeadlineThread'

我尝试使用objdump -t ./build/beginner_tutorials/CMakeFiles/stepper_motor.dir/src/schedule_wrappers.c.o objdump文件,并得到:

./build/beginner_tutorials/CMakeFiles/stepper_motor.dir/src/schedule_wrappers.c.o:     file format elf64-littleaarch64

SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 schedule_wrappers.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .debug_info    0000000000000000 .debug_info
0000000000000000 l    d  .debug_abbrev  0000000000000000 .debug_abbrev
0000000000000000 l    d  .debug_aranges 0000000000000000 .debug_aranges
0000000000000000 l    d  .debug_line    0000000000000000 .debug_line
0000000000000000 l    d  .debug_str     0000000000000000 .debug_str
0000000000000000 l    d  .note.GNU-stack        0000000000000000 .note.GNU-stack
0000000000000000 l    d  .comment       0000000000000000 .comment

我很困惑为什么. text部分是空的。
编译器命令cd /home/spovilus/catkin_ws/build/beginner_tutorials && /usr/bin/cc -DROSCONSOLE_BACKEND_LOG4CXX -DROS_BUILD_SHARED_LIBS=1 -DROS_PACKAGE_NAME=\"beginner_tutorials\" -I/home/spovilus/catkin_ws/devel/include -I/home/spovilus/catkin_ws/src/beginner_tutorials/include -I/opt/ros/noetic/include -I/opt/ros/noetic/share/xmlrpcpp/cmake/../../../include/xmlrpcpp -I/home/spovilus/catkin_ws/src/beginner_tutorials/yaml-cpp/include -I/home/spovilus/catkin_ws/src/beginner_tutorials/boost/units/include -O2 -g -DNDEBUG -o CMakeFiles/stepper_motor.dir/src/schedule_wrappers.c.o -c /home/spovilus/catkin_ws/src/beginner_tutorials/src/schedule_wrappers.c发出警告:

/home/spovilus/catkin_ws/src/beginner_tutorials/src/schedule_wrappers.c: In function ‘wrapper_sched_set_deadline’:
/home/spovilus/catkin_ws/src/beginner_tutorials/src/schedule_wrappers.c:35:7: warning: implicit declaration of function ‘perror’ [-Wimplicit-function-declaration]
   35 |       perror("Unable to get scheduler attributes");
      |       ^~~~~~
/home/spovilus/catkin_ws/src/beginner_tutorials/src/stepper_motor.cpp: In function ‘void positionCallback(const ConstPtr&)’:
/home/spovilus/catkin_ws/src/beginner_tutorials/src/stepper_motor.cpp:34:23: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘const ConstPtr’ {aka ‘const boost::shared_ptr<const beginner_tutorials::RelativePosition_<std::allocator<void> > >’} [-Wformat=]
   34 |     printf("position %d\n",position);
      |                      ~^
      |                       |
      |                       int

所以我得假设它起作用了。

源码

/**
 * @file schedule_wrappers.c
 * @author Sam Povilus (povilus@povil.us)
 * @brief A file to wrap Linux Scheduler calls
 * @version 0.1
 * @date 2023-01-07
 *
 * @copyright Copyright (c) 2023
 *
 */

#include <linux/ioctl.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <linux/sched/types.h>

#include "schedule_wrappers.h"

static int wrapper_sched_getattr(pid_t pid, struct sched_attr *attr,
                         unsigned int size, unsigned int flags)
{
    return syscall(__NR_sched_getattr, pid, attr, size, flags);
}

static int wrapper_sched_set_deadline(int deadline)
{
    struct sched_attr attr;
    int ret = wrapper_sched_getattr(0, &attr, sizeof(attr), 0);
    if (ret < 0)
    {
      perror("Unable to get scheduler attributes");
      // throw scheduler_exception;
    }
    attr.sched_policy = SCHED_DEADLINE;
    attr.sched_deadline = deadline;
    return syscall(__NR_sched_setattr, 0, attr, 0);
}

static int wrapper_sched_set_runtime(int runtime)
{
    struct sched_attr attr;
    int ret = wrapper_sched_getattr(0, &attr, sizeof(attr), 0);
    if (ret < 0)
    {
      perror("Unable to get scheduler attributes");
      // throw scheduler_exception;
    }
    attr.sched_policy = SCHED_DEADLINE;
    attr.sched_runtime = runtime;
    return syscall(__NR_sched_setattr, 0, attr, 0);
}


static int wrapper_sched_yield()
{
    return syscall(__NR_sched_yield);
}

cc版本

$ /usr/bin/cc --version
cc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
wfveoks0

wfveoks01#

为什么目标文件的. text部分是空的?
因为所有的函数都是静态的,所以它们被优化掉了。
未定义对"wrapper_sched_yield"的引用
If you want the function to be externally visible, remove static . Also note extern "C" needed between C <-> C++ interoperability.
警告:函数"perror"的隐式声明
您没有包括stdio. h
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘const ConstPtr’ {aka ‘const boost::shared_ptr<const beginner_tutorials::RelativePosition_std::allocator >’}
不知道你期待这里发生什么。代码是无效的,你不能printfboost::shared_ptr

相关问题