Compiling and linking

Is it possible to compile two files separately and then link them to create an executable ?
If yes, how to do it ?

This was my crude effort to do it
File : driver.cpp
1
2
3
4
5
6
7
8
int func();

int main()
{
  int ret_status;
  ret_status = func();
  return ret_status;
}


File : function.cpp
1
2
3
4
5
6
#include <iostream>
int func()
{
    std::cout<<"Let's see if it works";
    return 0xCAFEBABE;
}


and compiled the code by :
1
2
3
gcc.exe -c driver.cpp
gcc.exe -c function.cpp
gcc.exe -o lol.exe driver.o function.o
Last edited on
The GNU C++ compiler driver is g++

1
2
3
g++ -std=c++11 -Wall -Wextra -pedantic-errors -c driver.cpp 
g++ -std=c++11 -Wall -Wextra -pedantic-errors -c function.cpp 
g++  -o lol.exe driver.o function.o
Worked fine with new compilers.
How do I do it if the driver.cpp was compiled by gcc with extension .o and the function.cpp was compiled by some old compiler with extension .obj ?
When I tried to link with g++.exe it said file format not supported.
Thanks in advance.
There are more than one way to create object files (the things containing compiled code that will be linked into an executable). Microsoft and GNU do it different ways, and while the code is the same, the file formats are not compatible.

But the main problem is that the C++ Standard does not mandate name mangling rules, so MS and GNU do it differently. Meaning that unless your obj file has a C interface, it is unlikely that you can get it to work easily.

That said, it believe it can be done. (I just don't know how.)

Is it not possible to recompile the obj from sources?

Or can you not just use VC++ to compile the whole thing?

Sorry for the unhappy answer.
Topic archived. No new replies allowed.