简体   繁体   中英

How do I link multiple files without the use of a makefile?

For example, I'm given carModels.cpp , carModels.h , carType.in , manufacturers.h , manufacturers.o , and lastly my own file tester.cpp . How would I go about linking all of these using g++ in a Linux terminal? Would I have to create any additional ".o" files? I'm supposed to assume that the given files already work. Multiple lines in terminal are fine, I just I want a clear understanding of it. (I'm coming from a C++ IDE that I didn't really care for.)

you can do this in two steps, first compile to *.o files,

 gcc -c your.cpp other.cpp .....

then link them

gcc -o you_out_put_name the_object_files.o ...

Compile each source file to its own object file:

g++ -I . -c carModels.cpp -o carModels.o
g++ -I . -c tester.cpp -o tester.o

Now link all object files together:

g++ carModels.o tester.o manufacturers.o -o outputname

Consider adding more options like -O3 , -std=c++11 , -Wall , etc. as needed.

In a single line, that would be just g++ -o tester *.cpp *.o . GCC will sort everything out. In particular, the *.h files are referenced via #include "" statements in the .cpp files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM