0

I am running my programs in Ubuntu using a library names cpt. I am including the required header files from the library but the program does not work because it is unable to access the functions in the header file. ( it shows that error ) I have to include the .cpp files as well which contains the function's complete definition. I am now running my programs by also including the .cpp files associated with the required headers. Why I am getting this error inspite of the fact that I have -I/Desktop/cpt while compiling ?

cleanplay
  • 271
  • 4
  • 14
  • I have no idea what you're trying to say. There are a whole lot of steps between including header files and programs not working. – Wug Jul 09 '13 at 17:20
  • I just edited it for better explanation. – cleanplay Jul 09 '13 at 17:31
  • Are you including the headers for libraries but not linking against the libraries? Are you getting linker errors "function definition not found"? – Mooing Duck Jul 09 '13 at 19:20
  • Yes I am getting "undefined reference to -function- " error. Linking against the libraries- do you mean including the path to the .cpp files while executing the program ? – cleanplay Jul 10 '13 at 08:04

3 Answers3

0

You should not include .cpp files, they should be compiled, you should specify to your compiler which .cpp files to compile, and where to find the .h files the .cpp files require.

vladimirm
  • 261
  • 1
  • 2
  • 8
  • In my program I included the header "linalg.hpp" and while compiling, I used "g++ lin.cpp -o linex -I/Desktop/cpt" where cpt is the folder where linalg.cpp is located. How to compile all the .cpp files that are needed by my program in one execution ? – cleanplay Jul 09 '13 at 18:28
  • You need a makefile, a makefile consists of all the commands that you would give your compiler (g++ for example). When you create a makefile you can compile your code by just typing "make" in your console. You also have the ability to tell the compiler to compile all .cpp files in a directory, with the wildcard command, for example to compile all .cpp files in the source directory you would type: $(wildcard source/*.cpp), your makefile could look like this: SRC = $(wildcard source/*.cpp) LIB = -some_Lib_that_you_need all: g++ $(SRC) -o outputName $(LIB) – vladimirm Jul 11 '13 at 10:56
0

Maybe you should link your program with the corresponding shared/static library that provides the implementation of those functions declared in the header file?

Michael S.
  • 51
  • 3
  • What does that mean 'link program' - do you mean to mention the Include path while compiling or anything else ? I am already including the path to the .cpp files associated with the header files containing the functions I need . – cleanplay Jul 10 '13 at 08:03
  • I mean that maybe .cpp files ares supposed to be already compiled and given to you as a library, which you must link with your code when you create your final executable file. Is that your case ? – Michael S. Jul 10 '13 at 13:23
0

Vladimirm is correct, you do not need to #include the .cpp files. the header files should compile with or without their associated .cpp files. All of the .cpp files are linked together in a process known as linking, but the header files provide prototypes for functions during linking.

Are you using angle brackets around your #include? e.g.

#include <mylib.h> 

If so, you might consider switching to double quotes, e.g.

#include "mylib.h"

Depending on where the source files you are referencing are located, this could be the issue.

Owl_Prophet
  • 362
  • 5
  • 15
  • I am using "filename.h" . My header files and the associated .cpp files are located in the same directory in which I am running my program. Also while compiling, I am mentioning the include path. Still the program does not call the functions in the header file. I have to include the .cpp files and only then, the program calls the required functions in the header file. That is what I want to know why ? – cleanplay Jul 10 '13 at 08:01