declapp auto
declapp auto

Reputation: 678

qmake - changes in configuration are not detected and the library doesn't recompile

I want to choose the files that are compiled into a static library based on a configuration.

To do that, I have created two configurations (A and B) in the QtCreator's Projects tab and in my .pro file I have

CONFIG(USE_A){
    SOURCES += My_lib_a.cpp
}
CONFIG(USE_B){
    SOURCES += My_lib_b.cpp
}

and it works, but there is a problem.

When I switch between A and B configurations a couple of times, after both of those versions have been built, qmake doesn't detect that there has been a change anymore and the library is stuck with the old file. I'm forced to either touch the file that I want to recompile or manually choose to rebuild the library.

I've checked on Qt 5.12 and msvc 2017, but I need this to work on windows and linux.

Is this the correct way of doing this? Is there a way to overcome this problem?

Minimal example:

// example/top.pro
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
    my_lib\
    app

// example/app/app.pro
TARGET = app
SOURCES += main.cpp
INCLUDEPATH += ../my_lib
DEPENDPATH += ../my_lib
LIBS += -L../my_lib/debug -lmy_lib
PRE_TARGETDEPS += ../my_lib/debug/my_lib.lib

// example/app/main.cpp
#include "My_lib.h"
int main()
{
    My_lib m;
}

// example/my_lib/my_lib.pro
TARGET = my_lib
TEMPLATE = lib
CONFIG += staticlib
CONFIG(USE_A){
    SOURCES += My_lib_a.cpp
}
CONFIG(USE_B){
    SOURCES += My_lib_b.cpp
}
HEADERS += My_lib.h

// example/my_lib/My_lib.h
#pragma once
struct My_lib
{
    My_lib();
};

// example/my_lib/My_lib_a
#include "My_lib.h"
#include <iostream>
My_lib::My_lib()
{
    std::cout << "This is A" << std::endl;
}

// example/my_lib/My_lib_b
#include "My_lib.h"
#include <iostream>
My_lib::My_lib()
{
    std::cout << "This is B" << std::endl;
}

Upvotes: 2

Views: 400

Answers (1)

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

Since you build the library in the same build directory, with the same target name, you'll always come out with the latest built file, whatever the current build configuration is set to. To overcome the problem, you need to use separate build directories, one for each configuration.

In Build Settings, for each configuration, add %{CurrentBuild:Name} at the end of the Build directory path:

/somepath/example/%{CurrentBuild:Name}

Cleanup your current build directory, then build each configuration again. Given A and B your configuration names, you shold come out with a directory tree like this:

- somepath 

  - example

    - A                         
      + app
      + my_lib

    - B                         
      + app
      + my_lib

Upvotes: 1

Related Questions