链接错误与真正简单的函数C + +上的.h文件 [英] Link error with really simple functions C++ on .h file

查看:110
本文介绍了链接错误与真正简单的函数C + +上的.h文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了两个函数来把一个32/64位的指针转换成一个double。代码工作时,单独使用(只是.h和.cpp包括它),但当使用.h在别的地方(复制到项目目录,然后包括)ith throws'已定义'错误的所有功能。 h文件。



.h文件的源代码如下:

  #pragma once 
#ifndef __FLOATCAST_H
#define __FLOATCAST_H

//一个快速和脏的方式将指针转换为双精度和后退
/ /应该使用BOTH 64位和32位指针
union ptr_u {
double d;
void * p;
};

double ptr2double(void * pv){
ptr_u ptr;
ptr.p = pv;
return(ptr.d);
};

void * double2ptr(double dv){
ptr_u ptr;
ptr.d = dv;
return(ptr.p);
};

#endif

链接说明函数已在文件中定义源文件(而不是他的.obj),它不包括这一个。



编辑:
为什么我需要一个指针?因为我需要Lua(5.1)来回调一个对象成员函数。



Edit2:
Lua提供了一种存储用户数据的方法,

解决方案

标记您的函数内联

  inline double ptr2double(void * pv){
ptr_u ptr;
ptr.p = pv;
return(ptr.d);
};

inline void * double2ptr(double dv){
ptr_u ptr;
ptr.d = dv;
return(ptr.p);
};

你会发现,当你在两个独立的源文件多重定义。您通常有两个选项:




  • 在.h文件中定义函数,并在单独的.cpp文件中定义

  • 将您的函数内联并保存在.h文件中




#ifdef 防护多个包含到单个 源文件中。但你可以确保包括.h文件到不同的.cpp文件。 ODR适用于整个程序中的定义,而不只是一个文件。



EDIT2
在一些意见后,我觉得我必须纳入这条信息,这里,以免有任何误解。在C ++中,有关于内联函数和非内联函数的不同规则,例如ODR的特殊情况。现在,你可以标记任何函数(长或递归,无关紧要)作为内联,特殊规则将适用于它们。这是一个完全不同的事情,无论编译器将决定实际内联它(即,代替而不是调用代码),它可以做,即使你不标记功能为内联,并可以决定不做即使您将其标记为内联。


I've made two functions to 'cast' a 32/64 bit pointer into a double. The code worked when used alone (Just the .h and a .cpp including it) but when using the .h somewhere else (copied into the project directory and then included) ith throws 'already defined' error for all the functions on the .h file when linking.

the source code for the .h file is the following:

#pragma once
#ifndef __FLOATCAST_H
#define __FLOATCAST_H

//A quick and dirty way of casting pointers into doubles and back
//Should work with BOTH 64bit and 32bit pointers
union ptr_u {
    double d;
    void* p;
};

double ptr2double(void* pv){
    ptr_u ptr;
    ptr.p = pv;
    return (ptr.d);
};

void* double2ptr(double dv){
    ptr_u ptr;
    ptr.d = dv;
    return(ptr.p);
};

#endif

The link says the functions are already defined on a file source file (rather, his .obj) which does not include this one.

Edit: Why would I want a pointer-inside-double? Because I need Lua (5.1) to call-back an object member function.

Edit2: Lua provides a way of storing user data, it seems like the adequate soluton rather than casting the pointer (see comments)

解决方案

Mark your functions inline

inline double ptr2double(void* pv){
    ptr_u ptr;
    ptr.p = pv;
    return (ptr.d);
};

inline void* double2ptr(double dv){
    ptr_u ptr;
    ptr.d = dv;
    return(ptr.p);
};

You see, when you included the same function in two separate source files (translation units), you get multiple definitions. You have generally 2 options:

  • Have delrarations of your funcions in a .h file, and definitions in a separate .cpp file
  • Make your functions inline and keep them in a .h file

The One-Definition Rule of C++ prohibits multiple definitions of non-inline functions.

EDIT: The #ifdef guards guard against multiple inclusion into a single source file. But you can indeed include the .h file into different .cpp files. The ODR applies to definitions in the whole program, not just a single file.

EDIT2 After some comments I feel like I must incorporate this piece of information here lest there should be any misunderstanding. In C++ there are different rules concerning inline functions and non-inline ones, for example the special case of ODR. Now, you may mark any function (be it long or recursive, doesn't matter) as inline, and the special rules will apply to them. It is a completely different matter whether the compiler will decide to actually inline it (that is, substitute the code instead of a call), which it can do even if you don't mark the function as inline, and can decide not to do even if you mark it as inline.

这篇关于链接错误与真正简单的函数C + +上的.h文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆