tags:

views:

275

answers:

4

I have a mixture of c and c++ files compiling under g++. As explained in: http://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc

The c files are being compiled as c++ with the g++ command line. Not huge problem but migrating over to gcc will allow th c files to compile as c files and the c++ file to compile as c++.

What -I includes or -L libraries do I need to add to the gcc command line, that the g++ command line is including by default?

+1  A: 

You shouldn't need to add any. If it's using C++ it should automatically bring in C++ libraries.

If not, you'll want -lstdc++ (and if you're still getting undefined references, -lc for the libc). Don't forget -lm if you use math functions.

Matthew Iselin
Needed to include -lstdc++ for the linker command.
simon
+4  A: 

You shouldn't need to add any includes or libraries beyond what you already have.

Whatch out for C functions being called from C++ code - you need to tell the C++ compiler those are C functions so the program is linked correctly and works.

The standard practice is to add the following directives to all your C headers being included in C++ files:

#ifdef __cplusplus
extern "C" {
#endif

... C header contents go here ...

#ifdef __cplusplus
}
#endif

More info here: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

Kristoffon
Needed to include -lstdc++ for the linker command.
simon
A: 

Why not compile the c objects with gcc and the c++ with g++ and then when you link, link using the g++?

Los
I probably could, but I had a fairly complicated existing makefile with source from a 3rd party that I didn't want to have to monkey with.
simon
+1  A: 

GCC can determine which language a file is in based on the file extension. However, GCC does not automatically link in run time support for any language other than C. In practice that means you can compile C++ programs using gcc instead of g++ but you'll need to add the -lstdc++ directive:

#include <iostream>

int main()
{
    std::cout << "Hello world\n";
}

g++ hello.cc

gcc hello.cc -lstdc++

More accurately, you will need to specify -lstdc++ if you you use the standard library, exceptions, operator new, or RTTI. For instance, try compiling the following without -lstdc++:

int main()
{
    try {
       throw 1;
    }
    catch (int i)
    {
        return i;
    }
}

Please note that STL containers (including std::strings) use operator new by default. Strictly speaking you should be able to use the algorithms (std::min, std::find_first_of, etc.) binders and a few other things in the standard library without -lstdc++ but for the most part you might as well include it (the linker will ignore any libraries that you don't actually link to).

Max Lybbert
That kind-of makes sense. Because you're using the C++ standard library (std::cout), you should obviously include it in the command line. The real question is why you are using the C++ standard library in plain old C?
Gerco Dries
@Gerco Dries: C and C++ still have int main. What are you trying to say in your comment?
Matthew Iselin
@Greco, You'll notice that the last sentence of the question is "What -I includes or -L libraries do I need to add to the gcc command line, that the g++ command line is including by default?" which I understood to mean "if I have C++ code and I want to compile it with GCC, what switches do I need to pass for that to work?" So, yes, both program snippets are C++ snippets, and to compile them with GCC would require adding the command line switch -lstdc++.
Max Lybbert
I've edited the answer, hopefully this is clearer.
Max Lybbert
Yes I did need to include -lstdc++. I also needed to wrap my c function prototypes that were included in the c++ files with extern "C" as described in the other answers above. Your example illustrates this well.
simon