Hello,
I would like to implement my own functions in programs I make without having to define them in each program. I know I would have to make a library with the functions, but I don't know how to. If someone could explain or provide a tutorial on how to create your own libraries, it would be very helpful. Thanks in advance.
Thanks for the reply. So I have to link the regular .cpp files to the .o file which is the library file? Also, I heard that all you need to do is define the functions in the .cpp files. Is this true?
Err, not quite... .cpp files are compiled into .o files (object files) and you can add those to an .a archive (which is the static library), which is the one you have to link (although linking all the individual .o files would work too).
Also, I heard that all you need to do is define the functions in the .cpp files. Is this true?
Ok, I made a simple test.cpp file and added a function called getname() and defined it. Then I used g++ -c file1.cpp -o file1.o as you said to compile the file. A file called test.o appeared. Then I used ar r your_library.a file1.o file2.o to create a library called mylib. It created a file called mylib.a. Now I open a new source file called test2, add the iostream library and the mylib.a library and then I just call the getname() function in the main function. I compile and this error comes up:
test2.cpp:1:18: error: mylib.a: No such file or directory
test2.cpp: In function ‘int main()’:
test2.cpp:7: error: ‘getname’ was not declared in this scope
I tried linking the .o files to. Still compiles and the same error shows up.
Can't find out why this is happening. Help would be appreciated.