for example, i have a function declare in test.hpp and definition in test.cpp:
float sum(float x, float y)
i want to change function:
float sum(float x, float y,float z)
in clion,we can edit int this window:
in vscode, i should change function declaration in hpp and than chang functin definition in cpp which is really inconvient.
i have searched,but i only found that only visual studio 2022 support this.
Related
Here is my problem:
I need to design a program in F# that, from a C++ program, performs simple 3D vector calculations. I don't share the calculations on the codes below because the problem stops at creating the vectors according to the printfn.
So I think it's an error, but my schoolmate on macOS has no problem with this and can run the code correctly. I deduced that it was a Windows problem, probably because of the pointers. So I'm relying on you to help me.
Thanks a lot.
PS: dotnet version 7.0.101
clang version 15.0.5
Vector3.cpp
#include <math.h>
#include <iostream>
using namespace std;
int main()
{
return 0;
}
class Vector3
{
public:
double X;
double Y;
double Z;
Vector3(double x, double y, double z) : X(x), Y(y), Z(z) {}
};
extern "C" Vector3* CreateVector3(double x, double y, double z)
{
Vector3* v = new Vector3(x, y, z);
return v;
}
Program.fs
open System.Runtime.InteropServices
[<StructLayout(LayoutKind.Sequential)>]
printfn("test 1: Launching the program")
type Vector3 =
val mutable X: double
val mutable Y: double
val mutable Z: double
new(x, y, z) = { X = x; Y = y; Z = z }
[<DllImport("compiledVector3.exe")>]
extern Vector3 CreateVector3(double x, double y, double z)
[<DllImport("compiledVector3.exe")>]
extern double GetX(Vector3 v)
[<DllImport("compiledVector3.exe")>]
extern double GetY(Vector3 v)
[<DllImport("compiledVector3.exe")>]
extern double GetZ(Vector3 v)
[<DllImport("compiledVector3.exe")>]
extern double distanceTo(Vector3 v,Vector3 v2)
[<DllImport("compiledVector3.exe")>]
extern void vectorMovement(Vector3 v,double plusx, double plusy, double plusz)
[<DllImport("compiledVector3.exe")>]
extern Vector3 midpoint(Vector3 v,Vector3 v2)
[<DllImport("compiledVector3.exe")>]
extern double percentDistance(Vector3 pos1, Vector3 pos2, double percent)
printfn("test 2: DLL imported")
let FstVector= CreateVector3(0.0, 0.0, 0.0)
let SndVector= CreateVector3(1.0, 2.0, 3.0)
printfn("test 3: Vectors created")
I tried to launc the Program.fs with a basic dotnet run command, expecting to see in my terminal the following lines:
test 1: Launching the program
test 2: DLL imported
test 3: Vectors created
but there isn't the third line.
Terminal
PS C:\Users\AlexisLasselin\Documents\GitHub\2022-2023-project-3-harfang3d-binding-Project-4-group\CppToFs\Vector3> dotnet run
test 1: Launching the program
test 2: DLL imported
PS C:\Users\AlexisLasselin\Documents\GitHub\2022-2023-project-3-harfang3d-binding-Project-4-group\CppToFs\Vector3>
I think the compilation of your dll file is made for architecture x32 and you want to compile with a x64 windows architecture :
To compile a DLL using g++ for a 64-bit architecture, you can use the following command:
g++ -shared -o <filename>.dll -m64 <source files>
Replace with the desired name of the DLL, and with the names of the source files that make up the DLL.
The -shared option tells g++ to create a shared library (i.e., a DLL), and the -o option specifies the name of the output file. The -m64 option specifies that you want to compile for a 64-bit architecture.
I tried to find the definition of a function cblas_Xaxpy in Kaldi, so I was directed by GOTO Definition to the final place cblas-wrappers.h, where I found
inline void cblas_Xaxpy(const int N, const float alpha, const float *X,
const int incX, float *Y, const int incY) {
cblas_saxpy(N, alpha, X, incX, Y, incY);
}
Apparently the key is cblas_saxpy, first I tried to direct to the source file of this header file, but I did not find any. So I have tried search the whole directory and the parent directory concerning the project and I could not find any file containing the real definition of cblas_saxpy. But this is the original code and I ran it smoothly.
Then I am confused: if this is the correct version, then there should be some place to define the function cblas_saxpy's implementation, but where is it ?
cblas_saxpy() is defined in the LAPACK library. (As this is a C library, the source code does not need to be present to compile software against the library.) The definition of cblas_saxpy in that library is a wrapper around some extremely old Fortran code.
In Visual C++, if I change what a function, class, struct, etc. does in a source file but not the corresponding prototype in its header file, will all source files that use the function, class, struct, etc. need to be recompiled?
For instance, if I initially have something like this:
//function.h
int function(int x, int y);
//function.cpp
int function(int x, int y){
return x+y;
}
//main.cpp
#include function.h
int main(){
int x=3
int y=2
std::cout<<function(x, y);
return 0;
}
And then I change function.cpp to this:
//function.cpp
int function(int x, int y){
return x*y;
}
Will I need to recompile main.cpp?
No, you don't need to recompile main, because the ABI (Application Binary Interface) of your function didn't change. However, you need to link your main application against the recompiled function.cpp.
I have the following declaration in my code:
//Central diff function, makes two function calls, O(h^2)
REAL diff(const REAL h, const REAL x, REAL (*func)(const REAL))
{
// diff = f(x + h) - f(x -h)/2h + O(h^2)
return ((*func)(x + h) - (*func)(x - h))/(2.0*h + REALSMALL);
}
This goes in a "utils.h" file. When I compile a test using it it gives me:
clang++ -Weverything tests/utils.cpp -o tests/utils.o
In file included from tests/utils.cpp:4:
tests/../utils/utils.h:31:6: warning: no previous prototype for function 'diff' [-Wmissing-prototypes]
REAL diff(const REAL h, const REAL x, REAL (*func)(const REAL))
What I`m missing here ??
Since you defined (not declared) your function in a header then you should make it inline. Change:
REAL diff(const REAL h, const REAL x, REAL (*func)(const REAL))
to:
inline REAL diff(const REAL h, const REAL x, REAL (*func)(const REAL))
Or just move the definition into a .c file and keep just a prototype in the header file.
I'm trying to recompile my old game which links the Ruby library, but I keep getting this error:
ruby18-mt-static-release.lib(math.obj): error LNK2001: unresolved
external symbol _hypot
Is there any workaround for this that doesn't require me finding the source code to this library and rebuilding it?
I'm using Visual Studio 2010 and the latest DirectX SDK.
I had a similar problem. Apparently hypot used to be a linkable function but is now (I have VS 2010) an inline function that calls _hypot. In math.h this is the only function where this is done. I don't have source for the existing library I am using and it has hypot linked to it so it can't use the inline version. If I just define hypot myself the linker says it is already defined. The following works to fix this:
Edit math.h and comment or ifdef out the inline version of hypot.
Implement hypot as extern "C" double hypot(double x, double y) {return _hypot(x, y);}
Relink
Ugly, but it solved the problem.
You are using the MT-STATIC version of the library. You need to make sure your project (Code Generation->Runtime Library) is also set to multithreaded, not multithreaded DLL. Conversely, you can find the MT-DLL version of the library. Either way, the runtime library (either MT or MTD) must be consistent across your program and all libraries you want to link.
http://msdn.microsoft.com/en-us/library/abx4dbyh(v=vs.80).aspx
This is an old question but I have a new workaround that do not need to modify math.h.
I met a similar issue when I tried to static link 'msvcrt-ruby18-static.lib' into my own dll in Visual Studio 2012 (VS2012). I got the this error:
'unresolved external symbol __imp__hypot referenced in function _math_hypot'
Thanks to Matt's answer, we know it is caused by the change of 'math.h'.
This function:
double hypot(double _X, double _Y)
before vs2010 it was a dll export function declared by keyword like this:
extern "C" __declspec(dllexport) double __cdecl hypot(...)
since vs2010, it became an inline function:
static __inline double __CRTDECL hypot(...)
Luckily in VS2012, the inline function is wrapped by a macro RC_INVOKED. You can try this public domain implantation to let it link:
#define RC_INVOKED
#include <ruby.h>
extern "C" __declspec(dllexport)
double hypot(double x, double y)
{
if (x < 0) x = -x;
if (y < 0) y = -y;
if (x < y) {
double tmp = x;
x = y; y = tmp;
}
if (y == 0.0) return x;
y /= x;
return x * sqrt(1.0+y*y);
}
[NOTICE] My project is a DLL and I use the dllexport keyword directly. It seems the '__imp__' prefix cannot be defined directly. I tried to define a function named __imp__hypot(...) and I failed.
Implement hypot() yourself. It's pretty straightforward:
double hypot(double x, double y) {
double ax = fabs(x), ay = fabs(y);
double xy = x/y, yx = y/x;
return ax > ay
? ax * sqrt(1.0 + yx*yx)
: ay * sqrt(1.0 + xy*xy);
}