0

I have a C library made by cluster.h and cluster.c. I compiled that with gcc -c cluster.c.

I have to use a method of this library in the main class of a C++ project. This is the makefile i use:

abundancebin: main.o profileManager.o myHash.o myMalloc.o myStack.o myStringHash.o
    g++ -o abundancebin main.o profileManager.o myHash.o myMalloc.o myStack.o         myStringHash.o
main.o: main.cpp
    g++ -c main.cpp
profileManager.o: profileManager.cpp
    g++ -c profileManager.cpp
myHash.o: myHash.cpp
    g++ -c myHash.cpp
myMalloc.o: myMalloc.cpp
    g++ -c myMalloc.cpp
myStack.o: myStack.cpp
    g++ -c myStack.cpp
myStringHash.o: myStringHash.cpp
    g++ -c myStringHash.cpp

clean:
    -rm *.o abundancebin

I tried to import the C library in main.cpp using after other imports:

#ifdef __cplusplus
extern "C" {
#endif
#include <cluster.h>
#ifdef __cplusplus
}
#endif

but when i compile with make i have this response:

main.cpp:29:21: fatal error: cluster.h: No such file or directory #include <cluster.h> ^ compilation terminated. make: *** [main.o] Error 1

if i use "cluster.h" instead of i have this error:

main.o:main.cpp:(.text+0xf68): riferimento non definito a "kmedoids" main.o:main.cpp:(.text+0xf68): rilocazione adattata per troncamento: R_X86_64_PC32 contro il simbolo non definito "kmedoids" /usr/bin/ld: main.o: bad reloc address 0x18 in section.xdata' collect2: error: ld returned 1 exit status make: * [abundancebin] Error 1`

I also tried to copy the code part i need from C library to C++ project but the compiler reports many errors like this:

error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive] vector = malloc(nnodes*sizeof(int));

The library files are in the same folder of the project files. Can someone help? Thank you

leonardo vet
  • 119
  • 14
  • Try changing `` to `"cluster.h"`. (Quotation marks instead of angle brackets). The angle brackets tell the compiler to search in its own directory; the quotation marks tell it to start searching in the same directory where your source file is. – Logicrat Aug 19 '14 at 15:27
  • Or add `-I .` to your compile flags (to add the current directory to the list of searched include paths). – Cornstalks Aug 19 '14 at 15:34
  • Improve your `Makefile`. See e.g. [this example](http://stackoverflow.com/questions/14180397/creating-makefile-with-libraries/14180540#14180540) – Basile Starynkevitch Aug 19 '14 at 15:39
  • `invalid conversion from \`void*\` to \`int\`` is valid in C, put code causing this error in `extern "C"{}` – GingerPlusPlus Aug 19 '14 at 15:52
  • I'm sorry, i can't understand how to improve my makefile as the exaple – leonardo vet Aug 19 '14 at 15:54
  • You should be compiling C source as C using GCC instead of G++, then linking the object files into your C++ executable. – ButchDean Aug 19 '14 at 16:19
  • @GingerPlusPlus I tried but give me the same errors (invalid conversion from...) – leonardo vet Aug 19 '14 at 16:27
  • @ButchDean I already compile C source using gcc but i'm not able to link the object files in my C++ executable. Can you tell me how to to this more precisely? thank you – leonardo vet Aug 19 '14 at 16:29
  • @leonardovet Answered below for you. – ButchDean Aug 19 '14 at 16:50
  • Did you actually use command `gpp`? If so, that's one of your problems, you want `gcc`. If you did use `gcc`, please fix the question. – hyde Aug 20 '14 at 05:04
  • Also, in what directory you have the `cluster.*` files? If in the same dir, note that your `clean` target will remove `cluster.o`, yet you have no rule to re-make it. – hyde Aug 20 '14 at 05:15

2 Answers2

3

If you want to add some code which is wrote in c language, you have to #include it like:

extern "C" {   
    #include "x264.h"
}

which tell compiler to deal with it differently, and its not necessary to change your code to c++

1: you add your code as: #include <cluster.h> its better to change it to: #include "cluster.h" the different is, the second one tell the compiler to first search for your header in the current directory and then in the main c++ libraries directory

your error:

main.cpp:29:21: fatal error: cluster.h: No such file or directory

is because it can't find the header, so, if cluster.h is in the same directory as main.cpp, use #include "cluster.h" or you can use -I. (which tell its in the current directory) or -I/address to tell compiler where to look for your header

when you correct it, you get the second error, which I believe its because of your code, and I think its because of your code in main.cpp, and do not have anything with your cluster code, I suggest, try to post your code to find out what's the problem

Hadi Rasekh
  • 2,622
  • 2
  • 20
  • 28
-1

In answer to your question, I would approach it something like this in the makefile:

all: cfile.o
    g++ main.cpp -o app

cfile.o: cluster.c cluster.h
    gcc -c cluster.c

Note: This assumes that everything is in the same directory, so take care to specify paths if not.

Let me know of any further errors.

ButchDean
  • 70
  • 6
  • I'm sorry but i don't understant well what to write in the makefile. Can you modify mine (or part of it) to be more clear? It's my fault if I don't understand because I'm still not good at this and you would give me a big help! Thank you! – leonardo vet Aug 19 '14 at 18:24
  • @leonardovet I just simplified it for you. Compare with what you have done, as well as in particular what you are doing with your main that contains the entry point - you shouldn't be linking main.o to anything. Just follow the structure I have given. I can't fix it for you because I do not have your source, so don't know what is dependent on what. – ButchDean Aug 19 '14 at 18:31
  • @hyde Good spot! I forgot the output executable. Fixed the code. – ButchDean Aug 19 '14 at 18:34
  • You run the makefile again. – ButchDean Aug 19 '14 at 18:43
  • It all depends on the dependencies of the source files. I gave a simple example, but beyond the information given there is nothing to go on. For instance the example I gave depends on cluster.h being included in main.cpp - it was an example of how to structure the makefile, not necessarily magically fix everything. That is why I made the note of being notified of any specific errors. – ButchDean Aug 19 '14 at 18:51
  • Commands for target `all` will be done even if nothing is changed (because there's no file with that name, it is a *phony* target), which kind of defeats the purpose of using Makefile. `all` should usually depend on actual files, `app` in this case, and not have any commands under itself. Also, probably just "typo", but the command to build `app` does not include `ofile.o`. – hyde Aug 20 '14 at 05:00