如何从另一个包含目录中的标头访问包含目录中的标头?

时间:2019-06-12 10:53:29

标签: c++ cmake

我试图在一个目标中使用target_include_directories包含多个目录,但是一个包含目录中的标头无法访问另一个包含目录中的标头。我该如何解决这个问题?

我尝试将头文件放入add_library,但这无济于事。 我刚才也在这里搜索了两个小时,但似乎找不到任何东西。

我正在使用VC ++。

以下项目被简化,实际项目为here

项目结构:

project
|-- CMakeLists.txt
|-- include
|   |-- project_header.h
|-- subproject
|   |-- CMakeLists.txt
|   |-- include
|   |   |-- subproject_header.h
|   |-- ...
|-- ...

project/CMakeLists.txt

project("example")
add_subdirectory("subproject")

project/subproject/CMakeLists.txt

add_executable(example "${PROJECT_SOURCE_DIR}/subproject/source/main.hpp")
set_target_properties(example PUBLIC_HEADER "${PROJECT_SOURCE_DIR}/subproject/include/subproject_header.h" PRIVATE_HEADER "${PROJECT_SOURCE_DIR}/include/project_header.h"
target_include_directories(example PUBLIC "include" PRIVATE "${PROJECT_SOURCE_DIR}/include")

问题是当我在project/subproject/include/subproject_header.h中键入此内容时:

...
#include "project_header.h"
...

这将产生错误:fatal error C1083: Cannot open include file: 'project_header.h': No such file or directory

我该如何解决这个问题?

编辑:

使用..而不是${PROJECT_SOURCE_DIR}后,问题似乎已经解决。然后,我将其更改回${PROJECT_SOURCE_DIR},它仍然有效。这与缓存有关吗?

1 个答案:

答案 0 :(得分:0)

让我们说我的项目是这样的:

/** using for auto spin */
//@property(CustomButton)
autoSpin:CustomButton = null;

rollingCounts:number = 0;

stopUpdate:boolean = true;

spincontroller:SpinController;

gameController:GameController;


public autoSpinCall() {

}

/**
 * start or stop spin
 */
public reelspins() {
  SoundManager.getInstance().stopSoundEffects();
  SoundManager.getInstance().playSoundEffect(SoundManager.SOUND.SPIN_START);
  this.gameController.spinCounterLabel.node.opacity = 0;
  this.gameController.stopWinnerTextAnim();

  this.isSpinStart = !this.isSpinStart;

  if (ConfigManager.getInstance().seletedGamePlugin.gameOnReelSpinStarted) {
    ConfigManager.getInstance().seletedGamePlugin.gameOnReelSpinStarted();
  }

  this.gameController.startSpin.disableButton();
  this.gameController.startSpin.showDollerIcon(false);
  this.schedule(this.reelCountUpdate,0.1);

  const updateSpin = (reel, index) => {
    reel.stopAnimationTime = 0.0;
    reel.stopAnimation();
    reel.node.stopAllActions();
    reel.currentSpinningState=reel.ReelSpinStateEnum.IDLE;
    const spinStartWithDelay = cc.callFunc(() => {
      if (reel.currentSpinningState==reel.ReelSpinStateEnum.IDLE) {
        reel.startSpin();
      }
    }, this);
    const duration = this.gameController.startSpin.isTurboPlayStart ? this.reelTurboEndAnimationDurations[index] : this.reelEndAnimationDurations[index];
    reel.node.runAction(cc.sequence(cc.delayTime(duration),spinStartWithDelay));
  };
  this.reels.forEach(updateSpin.bind(this));
  this.gameController.hideButtonsOnSpinClick();
}

      // if autospin -> spin the reels again
  this.unschedule(this.spinTheReelsAgain);
  this.scheduleOnce(this.spinTheReelsAgain ,(durations[this.reels.length - 1] + 0.2));
}

    /**
* Start spinning if Auto/Turbo play is enable
*/
public spinTheReelsAgain(dt) {
  this.gameController.startSpinningWithDelay(0.15);
}

public balanceCreditWin() {
  this.isWinAnimationStart=false;
  PlayerDataManager.getInstance().spinFinished();
}
public updateReelContainerLayout() {
  // const layout: cc.Layout = this.reelContainer.getComponent(cc.Layout);
  // layout.updateLayout();     
}

project |---include | |---common.h |---library |---executable

project/CMakeLists.txt

project(example) add_subdirectory(library) add_subdirectory(executable)

project/library/CMakeLists.txt

add_library(lib ...) target_include_directories(lib PRIVATE "../include")

project/executable/CMakeLists.txt

如果我尝试从add_executable(exe ...) target_link_libraries(exe lib) 中的文件访问common.h,则编译器将抱怨上述错误。这是由于PRIVATE include标头不会包含在其依赖项中。

解决方案是将其添加到executableproject/projectB/CMakeLists.txt

相关问题