Identifier defined by a combination of headers? - c++

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.

Related

Main method is different to those in tutorials

So I am starting with c++ (i am trying to broaden my mind with new languages) but I came across a little issue which confuses me more than what I guess it should...
Using Visual Studio Express 2012, I created a console win32 application in C++ and this is my main method decleration:
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
however, since I dont know anything about c++, I searched for some tuts online and all of them had there declerations setup in a different manner
#include <iostream>
using namespace std;
int main()
{
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cin.get();
}
and
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
I tried typing in the "std::cout", but it wouldnt accept it,
could someone just clarify why and the significance of the difference ?
The main method can be define with or without parameters. It all depends upon what you are using your application for.
Take a look at this: https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fmainf.htm
Also for your program you need to have a return value
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
is (at least I think so) a Windows only library and compiler depending way of declaring the main function.
Definitly not wrong is to declare main like this:
int main(int argc, char const *argv[])
{
//do something
return 0;
}
or like this:
int main()
{
//do something
return 0;
}
This is definitly proper C++ and you can use this universially.
C++ programs may have one of two beginnings:
int main(int argc, char *argv[])
or
int wmain(int argc, wchar_t *argv[])
The first of these gets its arguments (argv) as ANSI charachers, while the second gets "wide" characters -- generally UTF-16 or UTF-32, depending on the platform.
Microsoft define a framework to allow you make code that can compile with either ANSI or wide characters.
int _tmain(int argc, TCHAR *argv[])
Behind the scenes, they have something like this:
#if defined UNICODE
#define _tmain wmain
#define TCHAR wchar_t
#else
#define _tmain main
#define TCHAR char
#endif
They also have helper functions like _tprintf() and _tcscpy().
NOTE: as pointed out by others, the argc and argv params are optional, so you can also have
int main()
and
int wmain()
and (for Microsoft and compatible compilers)
int _tmain()
Also note that while _tmain() is not strictly portable, you can easily create your own #define macros if you want to be portable to other platforms.

Using SDL2_gfx issues using 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"
}

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.

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);
}

C++ NetBEAN cout Error?

#include <cstdlib>
using namespace std;
/*
*
*/
int main(int argc, char** argv)
{
cout << "COME AT ME BRO!\n"
return 0;
}
It says cout is unable to resolve identifier
The C++ code assistance is setup properly, I'm just not sure whatelse it could possibly be.
You did not include <iostream> and thus the identifier std::cout is never declared or defined in your program.
You're including the wrong header file. It should be :
#include <iostream>