相关疑难解决方法(0)

为什么C++中的多重定义错误不是由const int声明引起的?

我有一个头文件foo.h:

#ifndef __FOO_H__
#define __FOO_H__

const char* USB_MANAGER_DBUS_SERVICE =  "com.USBService";
#define  USB_MANAGER_DBUS_OBJ_PATH  "/com/USB/MgrObject"
const int  DBUS_CONNECTION_MAX_RETRY_TIME  = 5;

#endif
Run Code Online (Sandbox Code Playgroud)

以及许多包含foo.h.

foo.c bar.c
Run Code Online (Sandbox Code Playgroud)

当它们一起编译时,出现“多重定义错误”。

Linking CXX shared library:
foo.cpp.o:(.data.rel.local+0x0): multiple definition of `USB_MANAGER_DBUS_SERVICE'
bar.cpp.o:(.data.rel.local+0x0): first defined here
Run Code Online (Sandbox Code Playgroud)

所以我有以下两个问题:

  1. 为什么不会#define导致链接错误?
  2. 为什么不会const int导致链接错误?

c++ linker

2
推荐指数
1
解决办法
1475
查看次数

extern函数原型和静态定义

我正在努力让FreeGLUT在OSX上构建,并且遇到了同样问题的许多实例.许多功能仅存在于.c文件中.

这是一个例子

extern void fghRestoreState( void );

static void fghRestoreState( void ){...}
Run Code Online (Sandbox Code Playgroud)

我对C的理解有限,但编译器错误似乎有意义:

src/Common/freeglut_gamemode.c:252: error: static declaration of ‘fghRestoreState’ follows non-static declaration
src/Common/freeglut_gamemode.c:43: error: previous declaration of ‘fghRestoreState’ was here
Run Code Online (Sandbox Code Playgroud)

我的问题是,他们是否有任何理由这样设置?它会在其他平台上正确编译吗?

c macos gcc freeglut

1
推荐指数
1
解决办法
3169
查看次数

在C中,extern存储类说明符的重要性和用法

可能重复:
C++中的外部链接和内部链接是什么

其实我想知道它的重要性extern.

首先我写了一些代码:

file1.h

extern int i;
Run Code Online (Sandbox Code Playgroud)

file2.c中

#include<stdio.h>
#include "file1.h"
 int i=20;
int main()
{
   printf("%d",i);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:当我必须定义iin 时,extern变量的用途是什么file2.c,声明file1.h是对我有用的方式.

sudhanshu

c c++

1
推荐指数
1
解决办法
1295
查看次数

全局变量多重声明

我在类的头文件中包含一个带有全局增强信号的 .h 文件,该类的头文件又包含在主函数的文件中。链接器说信号被多次声明。信号声明包含在典型的 C/C++ 头文件的#ifndef,#define和#endif块中(用于避免多重声明)。我正在使用带有 gcc 的 Eclipse。

#ifndef SIG_HEADER
#define SIG_HEADER
#include <boost/signal.hpp>

boost::signal0 <void> signal1;

#endif
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c++ boost

1
推荐指数
1
解决办法
1316
查看次数

c ++更新static int

我正在尝试更新static int

test.h

static int Delay = 0;
void UpdateDelay();
Run Code Online (Sandbox Code Playgroud)

TEST.CPP

#include "test.h"

void UpdateDelay(){
Delay = 500;
}
Run Code Online (Sandbox Code Playgroud)

Main.cpp的

    #include "test.h"

    int main(){
    UpdateDelay();
    std::cout << Delay << std::endl;
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

输出应为:500,但显示:0

谢谢

c++

0
推荐指数
1
解决办法
133
查看次数

在将其包含在头文件中时,为什么不需要为extern const(int)赋值?在c ++中

通常在声明任何类型的const时,你应该立即为它分配一个值,而不是在头文件中?为什么?

c++

0
推荐指数
1
解决办法
118
查看次数

声明和定义函数静态将产生“对function_name()的未定义引用”

这是在utils.hpp(NON OOP,所以不在任何类中)定义的函数原型声明

static void create_xy_table(const k4a_calibration_t *calibration, k4a_image_t xy_table);

static void generate_point_cloud(const k4a_image_t depth_image,
                                 const k4a_image_t xy_table,
                                 k4a_image_t point_cloud,
                                 int *point_count);

static void write_point_cloud(const char *file_name, const k4a_image_t point_cloud, int point_count);

Run Code Online (Sandbox Code Playgroud)

我对它们的定义utils.cpp包括utils.hpp。

当我使用中的函数调用任何main函数时main.cpp,出现以下错误:

/tmp/ccFcMfYB.o: In function `main':
main.cpp:(.text+0x208): undefined reference to 
`create_xy_table(_k4a_calibration_t const*, _k4a_image_t*)
Run Code Online (Sandbox Code Playgroud)

编译命令:

g++ -o build/testtest src/main.cpp src/utils.cpp -I./include -lk4a

将所有函数都定义为非静态,并且可以完美地进行编译!!我无法解决这个问题。

  • 到底是什么情况?
  • 如果需要定义和链接静态函数,应该怎么做才能正确包含/链接?

我的系统配置:gcc版本7.4.0(Ubuntu 7.4.0-1ubuntu1〜18.04.1)

编辑:这是我在utils.cpp中的函数定义:

static void create_xy_table(const k4a_calibration_t *calibration, k4a_image_t xy_table)
{
    k4a_float2_t *table_data …
Run Code Online (Sandbox Code Playgroud)

c++ g++ linker-errors

0
推荐指数
1
解决办法
52
查看次数

静态成员函数中对静态成员变量的未定义引用

这是我的头文件:

class MapObject: public ScreenObject {
    static float xoffset, yoffset;
public:
    static float Getxoffset() {
        return xoffset;
    }

};

#endif // MAPOBJECT_H
Run Code Online (Sandbox Code Playgroud)

但是在线返回xoffset; 我得到以下错误:未定义引用`MapObject :: xoffset'为什么?

c++

-2
推荐指数
1
解决办法
1120
查看次数

C++.我们可以有相同的命名功能,但在不同的文件中?

可以说我有三个.cpp文件和两个头文件.

第一档:

它是main.cpp,它调用所有函数和东西.包含2个头文件.


第二档:

包含两个功能:

int print(int num2, int num1)
Run Code Online (Sandbox Code Playgroud)

和

int update(int num1)
Run Code Online (Sandbox Code Playgroud)

第3档:

包含三个功能:

int calculate(int num2, int num3, int num4)
Run Code Online (Sandbox Code Playgroud)

和

int update(int num2, char random)
Run Code Online (Sandbox Code Playgroud)

和

int divied(int all)
Run Code Online (Sandbox Code Playgroud)

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++

正如您所知,第二个和第三个文件具有一个相同的命名函数"int update",但在一个文件中它有1个参数,而在另一个文件中它有两个.

我能打电话给我想要的人吗?我会收到编译错误吗?我现在无法测试它,因为我正在使用库计算机.

c++ compiler-construction function

-3
推荐指数
1
解决办法
174
查看次数

标签 统计

c++ ×8

c ×2

boost ×1

compiler-construction ×1

freeglut ×1

function ×1

g++ ×1

gcc ×1

linker ×1

linker-errors ×1

macos ×1