Creating a project, from Makefile to static/dynamic libraries in UNIX

前端 未结 6 955
予麋鹿
予麋鹿 2021-02-05 10:16

Guys, would you describe a few things about c++ building blocks, on unix.

I want to create an application that links against static libs and dynamic libs (.so).

<
6条回答
  •  我在风中等你
    2021-02-05 10:47

    Bare bones Makefile for creating a static library consisting of the code in foo.cpp, bar.cpp:

    PROJECT = library.a
    OBJECTS = foo.o bar.o
    CFLAGS  = -Wall -pedantic
    
    all: $(PROJECT)
    
    .cpp.o:
            g++ -c $(CFLAGS) $<
    
    $(PROJECT): $(OBJECTS)
            libtool -o $(PROJECT) -static $(OBJECTS)
    

    Bare bones Makefile for an app baz.cpp that static links to library.a:

    PROJECT = baz
    CFLAGS  = -Wall -pedantic
    OBJECTS = baz.o
    
    all: $(PROJECT)
    
    .cpp.o:
            g++ -c $(CFLAGS) $<
    
    $(PROJECT): $(OBJECTS) library.a
            g++ $(OBJECTS) -L. -llibrary -o $(PROJECT)
    

    Dynamic library left, ahem, as an exercise to the reader.

提交回复
热议问题