我试图在Qt应用程序中使用C ++库进行机械模拟。 (Project Chrono ,Git Hub source) 一切都很好,直到我尝试使用库的某些部分并得到一些错误。问题代码的一部分是:
ChGlobal.h
//
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2010-2011 Alessandro Tasora
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file at the top level of the distribution
// and at http://projectchrono.org/license-chrono.txt.
//
#ifndef CHGLOBAL_H
#define CHGLOBAL_H
#include <string>
#include "core/ChApiCE.h"
namespace chrono {
/// Set the path to the Chrono data directory (ATTENTION: not thread safe)
ChApi void SetChronoDataPath(const std::string& path);
/// Obtain the current path to the Chrono data directory (thread safe)
ChApi const std::string& GetChronoDataPath();
/// Obtain the complete path to the specified filename, given relative to the
/// Chrono data directory (thread safe)
ChApi std::string GetChronoDataFile(const std::string& filename);
} // END_OF_NAMESPACE____
#endif
ChGlobal.cpp
//
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2010 Alessandro Tasora
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file at the top level of the distribution
// and at http://projectchrono.org/license-chrono.txt.
//
#include <string.h>
#include "physics/ChGlobal.h"
#if defined(_WIN32) || defined(_WIN64)
#include "Windows.h"
#endif
#if defined(__APPLE__)
#include <libkern/OSAtomic.h>
#endif
namespace chrono {
// -----------------------------------------------------------------------------
// Functions for manipulating the Chrono data directory
// -----------------------------------------------------------------------------
static std::string chrono_data_path("../data/");
// Set the path to the Chrono data directory (ATTENTION: not thread safe)
void SetChronoDataPath(const std::string& path) {
chrono_data_path = path;
}
// Obtain the current path to the Chrono data directory (thread safe)
const std::string& GetChronoDataPath() {
return chrono_data_path;
}
// Obtain the complete path to the specified filename, given relative to the
// Chrono data directory (thread safe)
std::string GetChronoDataFile(const std::string& filename) {
return chrono_data_path + filename;
}
} // END_OF_NAMESPACE____
我在qt中的代码是:
const std::string& teste= chrono::GetChronoDataPath();
qDebug()<<"DataPath:"<<teste.c_str();
我得到一个奇怪的结果:
_________
Debugging starts
DataPath: ata/
Debugging has finished
__________
我只收到字符串的一部分。
然后如果尝试使用代码设置新路径:
const std::string& newpath("c:/");
chrono::SetChronoDataPath(newpath);
我得到了以下错误:
Exception at 0x74dfc42d, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) at f:\dd\vctools\crt\crtw32\heap\new.cpp:62
是因为chrono_data_path是静态变量吗?我尝试了另一种方法,但没有任何效果。我不是C ++专家,也许我错过了什么,
由于
答案 0 :(得分:0)
最简单的解决方案是转换库并将其与应用程序静态链接。
如果您坚持动态链接库,则可以使用线程安全的Q_GLOBAL_STATIC
机制:
// implementation (.cpp)
class chrono_data_path_t : public std::string {
public:
chrono_data_path_t() : std::string("../data/") {}
};
Q_GLOBAL_STATIC(chrono_data_path_t, chrono_data_path)
namespace chrono {
void SetChronoDataPath(const std::string& path) {
*chrono_data_path = path;
}
const std::string& GetChronoDataPath() {
return *chrono_data_path;
}
std::string GetChronoDataFile(const std::string& filename) {
return *chrono_data_path + filename;
}
} // namespace chrono
答案 1 :(得分:0)
我想出了问题,我正在将我的程序的调试版本与该库的发行版混合。库的Cmake配置为两个构建版本生成相同的名称。