Using SDL2_gfx issues using C++ - c++

When I use SDL2_gfx with the common.c /common.h files that are included with it, and then use the cpp instead of c extension using VS201X, I get the LNK2019: unresolved external symbol _SDL_main
What that means is if I change the file containing main to test.c, it compiles. When I change it back to text.cpp it fails to compile.
I think that indicates that it only works as C, and not C++.
Below is the code I copied from SDL2_gfxPrimitives.c (Spaces added so they would show up):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "common.h"
#include "SDL2_gfxPrimitives.h"
static CommonState *state;
int main(int argc, char* argv[])
{
/* Initialize test framework */
state = CommonCreateState(argv, SDL_INIT_VIDEO);
return 1;
}
I need to use the library in C++ but it seems I don't know enough to figure out how. Any help would be appreciated, I've spent two days attempting to figure this out.

This is a little bit of a gotcha for SDL.
What it does is #define main SDL_main
This renames int main(int argc, char *argv[]) into int SDL_main(int argc, char *argv[]).
In order to use it, SDL wants an unmangled SDL_main to link against, and because you simply renamed the test.c -> test.cpp, you didn't add the code to cause this to happen.
It's described in the documentation:
The application's main() function must be called with C linkage, and should be declared like this:
#ifdef __cplusplus
extern "C"
#endif
int main(int argc, char *argv[])
{
}
so if you put the:
#ifdef __cplusplus
extern "C"
#endif
immediately before the declaration of the main function, it should link correctly.

I figured out a fix. I had to put this code in. Also, the answer above is good. Now I know why it happens too. Google did not find that document.
extern "C"
{
#include "common.h"
}

Related

Identifier defined by a combination of headers?

Recently I ran into a identifier undefined error, but was surprised to find that it was because there were "two" imports missing, rather than one. I.E.
#include <a.h>
int main(int argc, char ** argv)
{
foo();
return 0;
}
would not compile because "foo" was undefined.
#include <b.h>
int main(int argc, char ** argv)
{
foo();
return 0;
}
also would not compile because "foo" was undefined.
however:
#include <a.h>
#include <b.h>
int main(int argc, char ** argv)
{
foo();
return 0;
}
was fine. I'm not sure how this is possible. Does anyone know what's going on here? Why is it that foo() is somehow defined by the combination of these two headers and not either of them individually?
For completeness, this happened during some fiddling with OpenGL on windows. the exact code that produced this behavior was:
#include <Windows.h>
#include <gl/GL.h>
int main(int argc, char ** argv)
{
glClear(1);
}
where glClear was undefined if either of the two headers was missing. For clarity, the error in both cases was "Error: Identifier "glClear" is undefined"
Using preprocessor directives, you can silently "hide" pieces of code in compile time.
For example, if a.h has:
#define A_INCLUDED
b.h may have:
#ifdef A_INCLUDED
void foo(void) { return; }
#endif
That way, b.h won't declare foo() unless a.h was included before.
I should look at those files to say for sure, but probably it's something like this.

Error with using argv with function

I'm beginning to learn cpp and I've started with a a simple program:
#include <stdio.h>
#include <vector>
#include "functions.h"
using namespace std;
int main(int argc,char * argv[]){
double start=0;
double end=0;
vector<double> fbseq;
getInput(argc,argv);
return 0;
}
and in functions.h, I have:
using namespace std;
void getInput(int,char **);
When I compile and run I get:
g++ fibonacci.cpp -o fibonacci
/tmp/ccZ1cCvi.o: In function `main':
fibonacci.cpp:(.text+0x3d): undefined reference to `getInput(int, char**)'
I don't really understand what this means, can someone help me as I can see this is related to me not understanding pointers?
Thanks
Dan
It's a linker error, which means that the code compiles fine but the linker can't find the definitions for everything you've used. In this case, it means you haven't defined getInput anywhere. You need to give the function a body. Typically, you should do this in a corresponding .cpp file:
#include "functions.h"
void getInput(int argc, char** argv) {
// Implement here
}
When you do have that, you need to make sure that you compile that file too. Just add it to the g++ command.
The problem is not about pointers, it is about you only declared the function getInput and it is not defined anywhere.
Your compilation command seems to miss functions.cpp or other file which implements getInput function.
P.S. You shouldn't write "using namespace std;" in header file, it's a bad practice. See Item 59 in Sutter and Alexandrescu's "C++ Coding Standards: 101 Rules, Guidelines, and Best Practices":
"Don’t write namespace usings in a header file or before an #include."
You have only declared getInput function (which is the part in the header file) but you have not actually defined it anywhere (specifying the function body).

ffmpeg in C++ "av_register_all not declared in this scope"

I'm trying to compile this simple program in C++ (in Code Blocks):
#ifdef __cplusplus
extern "C" {
#endif
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#ifdef __cplusplus
};
#endif
int main(int argc, char *argv[]) {
av_register_all();
return 0;
}
However I keep getting the error message:
|11|error: ‘av_register_all’ was not declared in this scope|
Other people seem to have had this problem and adding the extern "C" section seems to have solved it form them but not me. Does anyone have any suggestions?
Thanks
Did you set up your additional libraries / additional include files?
Here you can find how to do it in MSVC++, I have used CodeBlocks a little & never included an external library to it, however I think you should be able to adopt it to CodeBlocks.

C++ access function from C DLL/lib

I'm desperately trying to access a DLL supplied with some hardware. I have the DLL, the LIB file and the headers for the DLL.
I first tried using C# but failed due to the large data structures that are passed around. I got the functions called but the values in the structures that are modified are not correct.
I then thought about writing a wrapper class in C++ and use this as a library for C#. But again here I can call the function but if I tested an signed long that is passed it is "1072955392l" instead of "-1l".
I then just renamed the cpp file to ".c" and compiled it again. Now I get the correct value.
Are there some differences in the datatypes from C to C++?
The functions for the LIb in the supplied include file are declared like that:
_declspec (dllimport) long ResetControl(Registers* regs);
I compile using VS2013:
cl test.cpp /link test.lib
cl test.c /link test.lib
The cpp file and c file are the same unless I needed to include #include for the cpp and wrap the dll include header in
extern "C"
{
#include "test.h"
}
The test.c file looks like:
//#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <math.h>
#include "test.h"
int main (int argc, char **argv)
{
Registers Regs;
Reset (&Regs);
printf ("Value: %dl\n\r", Regs.Product);
return 0;
}
The C++ file:
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <math.h>
extern "C"
{
#include "test.h"
}
int main (int argc, char **argv)
{
Registers Regs;
Reset (&Regs);
printf ("Value: %dl\n\r", Regs.Product);
return 0;
}
Both compile, but the result of printing Regs.Products is different:
C: -1l
C++: 1072955392l
What am I doing wrong here?
I saw somewhere that there is a matter of aligning to 4 bytes while compiling the client of the DLL.
However have a look to those pages:
http://www.codeproject.com/Articles/21/Beginner-s-Tutorial-Calling-Visual-Basic-ActiveX-D
http://www.codeproject.com/Articles/6242/Step-by-Step-Calling-C-DLLs-from-VC-and-VB-Part
Alexandre

Why GetProcAddress doesn't work?

First, I create a simple dll called SimpleDll.dll, its head file:
// SimpleDll.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif
MYLIBAPI int Add(int a. int b);
its source code:
// SimpleDll.c
#include <windows.h>
#define MYLIBAPI __declspec(dllexport)
#include "SimpleDll.h"
int Add(int a, int b)
{
return a + b;
}
Then I call it in another project, and it works fine:
// TestSimpleDll.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"
#pragma comment(lib, "SimpleDll.lib")
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", Add(10, 30)); // Give the expected result 40
return 0;
}
However, when I call GetProcAddress to get it's address, it doesn't work!
// TestSimpleDll2.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"
#pragma comment(lib, "SimpleDll.lib")
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", Add(10, 30)); // Give the expected result 40
HMODULE hModule = GetModuleHandleA("SimpleDll.dll"); // hModule is found
PROC add_proc = GetProcAddress(hModule, "Add"); // but Add is not found !
// add_proc is NULL!
return 0;
}
Thanks for your help. (PS: I use VS2010 on Windows7)
Update:
This is what the depedency walker show for the SimpleDll.dll file:
You should use a .def file if you want to export the name for GetProcAddress. Otherwise you will have to deal with c++ name mangling and with symbol decorations.
You can avoid mangling by declaring your function as extern "C", but the only way to avoid decorations is to use a .DEF file.
One more thing - in Dependency walker - use F10 to toggle between decorated and undecorated names.
Dependency Walker is an excellent tool for troubleshooting DLL issues like this.
I'm assuming you are compiling the DLL as C code. Otherwise, C++ performs name mangling that would cause problems.
To avoid name mangling simply wrap the export definition in extern "C".
extern "C" {
MYLIBAPI int Add(int a. int b);
}