Suppose I have a function defined like this:
class Foo() {
public:
void bar(MyClass* p, int i, int j, CArray<CArray<int,int>,int> &a);
}
void Foo::bar(MyClass* p, int i, int j, CArray<CArray<int,int>,int> &a){
// Function body
}
For a Win32 application/DLL, that this function is not "exported" how can I be able to find the function address of bar, getting the function address of exported function was easy. However getting the function address of non-exported function is a bit hard.
It is not possible to do this in the general case.
Among other problems, if the function is not exported, then it may not exist. The optimizer may inline the function at every location where the function is called. If this occurs, the function won't have an address because it won't exist in the module.
if the functions are in the .dll, you can probably export them using a .def file. It creates an export table after the fact from compiled code as if dllexport had been defined.
Read about it here: http://msdn.microsoft.com/en-us/library/d91k01sh(v=vs.80).aspx
Related
In this question, the answer states that to inline a function for a static library, the function is declared inline in the header file and extern in the source file. However in C++, if this is done, a compiler error (Redeclaration of member is not allowed) is generated. What is the correct way to write a function in so it works the same way as in the C post?
Header:
class Int64
{
uint64_t a;
public:
inline void flip() { a = ~a; }
};
Source:
extern void Int64::flip(); // redeclaration of member is not allowed
In C++, you can declare a function as inline only if function code is available in compile time (if you really want to inline that function code). So you cannot implement function body inside compiled static library which will not be available when you use this static library. If you do so, this function call will be similar to a normal function call.
From cppreference:
2) The definition of an inline function must be present in the
translation unit where it is accessed (not necessarily before the
point of access).
Although, you can define your inline function in your static library header(like header only function).
By the way, remember that inline is just a suggestion. compiler will decide to inline or not. This normally happens when you compile code with optimizations enabled, but especially when you don't optimize your code, you normally see that functions are not inlined.
As an example, check this small static library with containing 2 files:
test.h:
#pragma once
inline int sum(int a, int b)
{
return a + b;
}
int sub(int a, int b);
test.cpp:
int sub(int a, int b)
{
return a - b;
}
When you use this library, sum will be inlined and sub will be a normal normal call. Remember, you can even define sub as inline in library header too (without its body) and it will still be like a normal function call.
If we do not mention the function prototype,call the function from the main and write the definition after main it gives an error.If we write the function definition before main and do not write the prototype the program works fine. So my question is if we write the function definition before main(without writing the prototype) does it solve the problem of not declaring a function prototype(i.e, the compiler will start reading from top-down and will still be able to know about the function name,return type,parameters and etc)
Defining the function with no prior prototype is semantically equivalent to declaring the prototype immediately before defining the function. So yes, it's safe: defining a function without a prototype, before any uses of the function, will work just fine.
Compiler goes at function definition when you make a call to that function. While function prototype is consulted for arguments and return type checking. So, you can safely emit function prototype in your case...
Yes (for C++), compiler will accept this. To call a function compiler must see its prototype or definition before. Also translation units (cpp) files are compiled from top to bottom, so:
void foo();
void foo2() {
}
int main() {
foo();
foo2();
}
void foo() {
}
are both correct. If you would provide foo() prototype without defining foo below main(), compiler will still accept your code, but linker would complain with error.
Basically, I have an already compiled a file that is written assembly; .obj file. It has an exported function f. In C++, I wrote a class and want to use the exported function f as a member function.
If it were to be used as a global function, I know that one just writesextern "C" f(). However, this doesn't work with a member function, at least I didn't figure out how to do it.
So, how do I do it?
Note: The function f is written properly. i.e. it takes into account the this pointer, etc. It should work correctly at assembly level.
If the function is written as a free function, you will have to declare it as a free function the way you're accustomed to. Just as you can't build a library with void foo() and then somehow declare it as void bar() and expect it to work, you can't take a free function and turn it into a member function. What you can do, though, is add an inline forwarder into your class like so:
struct S;
extern "C" void f( S * );
struct S {
void f() {
using ::f;
f(this);
}
};
That's the best you can do, as far as I'm aware.
this is the cpp of a Time function. The code is defining functions of a time.h file on this time.cpp. My question is: How come this function definition is possible if the functions inside this fct are defined afterwards? Thank you
void Time::setTime(int hour, int minute, int second)
{
sethour(hour);
setminute(minute);
setseconds(seconds);
}
void Time::sethour( int h)
{ ....
You don't need a definition to call a function, you only need a declaration. The compiler is happy with the declaration alone. The linker requires code to be generated, and it requires the definition, but it doesn't matter when you define them, as long as you do.
In your case, the declaration of every member function is visible to all other member function, even if inside the class definition it came afterwards:
class Time
{
void setTime(); //setTime knows about sethour even if it's before
void sethour();
};
Outside of a class, this doesn't hold, meaning you need a declaration before using a method. The declaration is just the prototype:
void foo();
void goo()
{
foo(); //can call foo here even if it's just declared and not defined
}
Presumably because they were declared somewhere above (e.g. in the header file), and that's what's important.
It's best to imagine that the compiler operates in a "one-pass" approach; it processes your code linearly from top-to-bottom. Therefore, it needs to know that functions exist (i.e. their name, arguments and return type) before they are used, in order to establish that the caller is not doing something invalid. But the actual function definition (i.e. its body) is not relevant for this task.
You can choose when to define them.
I have just started learning C++. Can some explain the difference between the following C++ function prototypes?
void f(int n);
extern void f(int n);
static void f(int n);
The void and extern void versions are the same. They indicate that the function has external linkage (i.e. the definition of the function may be expected to come from some other C or C++ file). Static indicates that the function has internal linkage, and will exist only in the current C++ file.
You almost never see these specifiers applied to functions because 99.9% of the time you want the default extern behavior.
You may see the static or extern storage specifiers on global variables though, which is often done to reduce name conflicts with other files in the same project. This is a holdover from C; if you're using C++ this kind of thing should be done with an anonymous namespace instead of static.
This is more of a C-language question than a C++ one, but:
void f(int n);
Declares a function f that takes a single integer parameter.
extern void f(int n);
Declares a function f that takes a single integer parameter but exists in some other file. The compiler will trust that you have implemented the function somewhere. If the linker cannot find it, you will get a linker error.
static void f(int n);
Declares a function f that takes a single integer parameter. The static keyword makes this interesting. If this is in a .cpp file, the function will only be visible to that file. If it is in a .h file, every .cpp file that includes that header will create its own copy of that function that is only accessible to that implementation file.
The first two are the same thing. The third one gives f internal linkage, meaning that a different source file may use the name f to be something different.
The use of static as in that third example should not be used. Instead use an anonymous namespace:
namespace { // anonymous
void f(int n);
}
Both answers so far have deprecated the use of static functions. Why? What makes
namespace {
void f(int n);
}
superior to
static void f(int n);
? It's not simpler, it's not easier to understand...
Anonymous namespace is a more universal and cleaner solution, you can have functions, variables, classes in it. And static is way too overloaded, in some contexts meaning internal linkage, in others static lifetime.
There is one disadvantage of anonymous namespace though. Because of external linkage, the exported section of your object/library files will swell with all those long <unique namespace name>::<function> names which wouldn't be there is they were static.