Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use cout/endl when extern "C"

Tags:

c++

extern

I hope someone can help me on this small problem as I'm getting no where at all. Firstly, heres the code:

#include<string>
#include<iostream>
#include<Windows.h>

using namespace std;

extern "C"
{
#include<hidsdi.h>
#include<SetupAPI.h>
}

int main()
{
    int iQuit;

    cout << "Testing Program" << endl;

    return 0;
}

It won't let me use the std stuff (ie cout, cin, endl etc). It will only allow me to use it when I take out all the extern "C" code... Why is that? And is there a way around this?

EDIT: Sorry, forget to tell you the exact error: It says identifier "cout" is undefined

Thanks

like image 473
Danny Avatar asked May 05 '12 18:05

Danny


People also ask

Can endl be used in C?

We can use endl in C++ but not in C. So endl runs well in C++ but if we use C, it runs error.

When to use std:: cout in c++?

std::cout is used to output a value (cout = character output) std::cin is used to get an input value (cin = character input) << is used with std::cout, and shows the direction that data is moving (if std::cout represents the console, the output data is moving from the variable to the console).

Why is Endl not declared in this scope?

Re: endl' was not declared in this scope If you meant std::endl then you have to say so (or use the using keyword). This is basic C++ and nothing at all to do with Qt. It won't compile either way because there is no QDataStream operator<<() suitable for std::endl.


2 Answers

There is something wrong with your compilation environment. There is nothing wrong with that code. It compiles and runs fine.

In a comment to the question you say:

I'm learning to write a driver so I can do stuff with my devices like keyboards and mouses, so I'm using the WDK. Correct me if I'm wrong, but I thought you need to use extern "C" to use those header files?

In fact you should simply write code in C rather than C++ for driver development.

like image 164
David Heffernan Avatar answered Oct 16 '22 12:10

David Heffernan


I've found out the problem, I weren't adding the directories in the correct order in VC++ Directories.

The order should be:

C:\WinDDK\7600.16385.1\inc\ddk
$(VCInstallDir)include
$(VCInstallDir)atlmfc\include
C:\WinDDK\7600.16385.1\inc\api
$(WindowsSdkDir)include
$(FrameworkSDKDir)\include

Don't know why it needs to be in this order, but it works now... Very strange...

like image 23
Danny Avatar answered Oct 16 '22 12:10

Danny