-1

I'm trying to compile a function in C++ and then take the output object file and compile it into a C function. I'm getting an error during linking. Here's the code:

row_executor.c:

#include "row_exec.h"

void row_executor(char* row) {
    row_exec(row);
    return;
}

row_exec.h

#ifndef ROWEXEC_CPP_H
#define ROWEXEC_CPP_H

#ifdef __cplusplus
//fill in C++ code here later
#endif

#ifdef __cplusplus
extern "C" {
#endif

  void row_exec(char* rowname);

#ifdef __cplusplus
} //end extern "C"
#endif

#endif

row_exec.cpp:

#ifdef __cplusplus
#include "row_exec.h"
#include <iostream>

using namespace std;

void row_exec(char* rowname) {
  cout << "Row name is: " << rowname << endl;
}

#endif

The C++ object file is generated by makefile entry:

row_exec.o: row_exec.cpp row_exec.h
    g++ -Wall -c row_exec.cpp

The C object file is generated by:

gcc -c -I<include paths> -g -fPIC -DPIC -o row_exec.o row_executor.c

Both generate object files fine, but when the row_executor.o gets linked together with a bunch of other unrelated object files corresponding to other tests, I get the following error:

row_executor.o(.text+0x1a): In function row_executor': /LAB/MODULES/rev_13~~/row_executor.c:4: undefined reference torow_exec' collect2: ld returned 1 exit status make: *** [hm_prod_rev_16.so] Error 1

Does anyone see the issue?

486DX2-66
  • 159
  • 2
  • 10

1 Answers1

1
row_exec.o: row_exec.cpp row_exec.h
    g++ -Wall -c row_exec.cpp
gcc -c -I<include paths> -g -fPIC -DPIC -o row_exec.o row_executor.c

your object file for row_exec.c and row_executor.c are the same. One of your object file get overwritten.

Phong
  • 6,600
  • 4
  • 32
  • 61
  • Thanks for your helpful response. I thought the -o flag was to specify an object file to include. As you pointed out, it actually specifies the output object file name. Maybe I don't need to include the C++ object file (row_exec.o) until the final linking stage. I'll try that and see what happens. – 486DX2-66 Feb 17 '16 at 02:02
  • yes you dont need other object file to compile (not link) other file. you just need them when you link all object file into one. Here the fact that c and c++ file are used together is irrelevent. – Phong Feb 17 '16 at 02:07
  • Update: Actually, I can't simply add the row_exec.o file in the final linking stage because the final linking is done using gcc and row_exec.o was compiled with g++. So somehow, I need to / want to compile/link row_exec.o "into" row_executor.c using g++. Then that output can be linked using gcc with the rest of the gcc-generated object files. Does anyone know how to do that? – 486DX2-66 Feb 17 '16 at 02:08
  • The fact that it was compiled with gcc or g++ is irrelevent. you are handling object file. gcc and/or g++ can do it. what error do you get ? I will try to reproduce it in my environement – Phong Feb 17 '16 at 02:10
  • Sorry it look like i make a mistake. Link the code with g++ and not gcc it should work. Now why does it not work ... it is maybe gcc does not use standard library c++ for linking ? – Phong Feb 17 '16 at 02:13
  • link using this command: `g++ row_exec.o row_executor.o Xxxxx.o` – Phong Feb 17 '16 at 02:16
  • I thought about doing the final linking with g++ instead of gcc. The reason I have not is because my code is being linked with about 50 other object files developed by other people (this is for an automated wafer tester and the final linking stage is setup to link everything together using gcc). So if I can simply modify the g++ part, that would be best. This FAQ recommends against compiling C with C++: https://isocpp.org/wiki/faq/mixing-c-and-cpp#overview-mixing-langs – 486DX2-66 Feb 17 '16 at 02:18
  • The link command above produces a few errors. Based on the output, it looks like using gcc with the lstdc++ argument produces the same result, ex. gcc -lstdc++ cplusplus.o -o myexe (http://stackoverflow.com/questions/1001535/linking-c-code-with-gcc-without-g) – 486DX2-66 Feb 17 '16 at 04:33