我有一个头文件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)
所以我有以下两个问题:
#define导致链接错误?const int导致链接错误?我正在努力让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++中的外部链接和内部链接是什么
其实我想知道它的重要性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
我在类的头文件中包含一个带有全局增强信号的 .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)
我究竟做错了什么?
我正在尝试更新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
谢谢
通常在声明任何类型的const时,你应该立即为它分配一个值,而不是在头文件中?为什么?
这是在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) 这是我的头文件:
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'为什么?
可以说我有三个.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个参数,而在另一个文件中它有两个.
我能打电话给我想要的人吗?我会收到编译错误吗?我现在无法测试它,因为我正在使用库计算机.