1
votes

I am in the process of changing a C file to a C++ file (to eventually integrate it with a C program). I'm very new to C++ and in fact this is my first exposure to it. I have a test.cpp file that declares function main and hello as follows:

#include "test.h"

int main()
{
    hello ();
    return 0;
}

void hello()
{
    std::cout << "Hello there!" << endl;
}

The test.h file is declared as follows:

#include <iostream>

extern "C" void hello();

When I compile the program using g++ test.cpp, I get the error "hello was not declared in this scope".

Any suggestions?

Also, where does one find the API for C++ classes and their functions?

3
What is the command you have fired?Sumeet
So if a C program, say myprog.c, needs to invoke the function hello(), I would declare that function in myprog.h as extern "C" void hello?NewToAndroid
Calling C++ from C is hard, at least harder that calling C from C++. Best to put all functions that don't require c++ in a separate .c source file, so you compile them as C, and declare them extern "C" from your C++ source files. Put some #ifdef __CPLUSPLUS around the "extern C" in .h files so you can include them from both languages; check the standard include files for examples.Guntram Blohm
Thank you for a complete compilable test case but can you please post the full text of the actual error that you are getting?CB Bailey
-1 The code in your question does not match the error messageDavid Heffernan

3 Answers

2
votes

I think that you may have misread the error message. The only mistake that should cause an error is the fact that you haven't qualified endl with std::. Are you sure that the error message wasn't about endl?

Compiling your complete test case, I get the following:

$ g++ test.cpp
test.cpp: In function ‘void hello()’:
test.cpp:11:37: error: ‘endl’ was not declared in this scope
      std::cout << "Hello there!" << endl;
                                     ^
test.cpp:11:37: note: suggested alternative:
In file included from /usr/include/c++/4.8/iostream:39:0,
                 from test.h:1,
                 from test.cpp:1:
/usr/include/c++/4.8/ostream:564:5: note:   ‘std::endl’
     endl(basic_ostream<_CharT, _Traits>& __os)
     ^

Fixing the error by adding the std:: to endl fixes all compile and link errors and gives hello C language linkage as expected.

(Note, there is no harm - and it's probably even clearer - to add extern "C" to the definition of the function hello, but it is not necessary so long as the first visible declaration declares the correct language linkage.)

1
votes

The problem is you're declaring it extern "C" in the include file, but it's in the hello.cpp source file, so it will get compiled as c++, not c.

1
votes

You should remove extern "C" altogether,
Use the standard namespace.

But if you must compile the function as extern "C", it is not required you put it before the function definition, only the declaration, which you have done.
But if you'd like to add it to both declaration and definition, it would be fine for you to do so.

Example:

#include "test.h"

using namespace std;    

int main()
{
    hello ();
    return 0;
}

void hello()
{
    cout << "Hello there!" << endl;
}