我想使用 cmake 来安装我的库,edv但是当我执行时:
cmake --build . --target install
Run Code Online (Sandbox Code Playgroud)
它会安装,但只会创建bin/edv.dll和lib/ < empty >。我怎样才能让 cmake 安装在EDV_PUBLIC_INCLUDE_DIRECTORIES里面include/...?
这是我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(edv)
# include PUBLIC directories
set(EDV_PUBLIC_INCLUDE_DIRECTORIES include/ )
set(EDV_PRIVATE_INCLUDE_DIRECTORIES src/ )
# Edv source files list
file(GLOB_RECURSE EDV_SOURCE_FILES "src/*.cpp" "src/*.hpp*")
# build the library
add_library(${PROJECT_NAME} SHARED ${EDV_SOURCE_FILES} )
target_include_directories(${PROJECT_NAME} PUBLIC ${EDV_PUBLIC_INCLUDE_DIRECTORIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${EDV_PRIVATE_INCLUDE_DIRECTORIES})
install (TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
Run Code Online (Sandbox Code Playgroud) 此代码编译并执行.我知道在第一种情况下我们有未定义的行为.但在第二种情况下究竟发生了什么?
#include <string>
#include <iostream>
#include <cstdio>
std::string foo() {
return "HELLO";
}
void bar(const char *p) {
std::printf("%s\n", p);
}
int main() {
// FIRST CASE:
// I know this is bad, because after the assignment
// the variable returned by foo() is destroyed and we
// have a bad reference.
const std::string &s = foo();
bar(s.c_str());
// SECOND CASE:
// But what about that ? I don't know exactly if the
// object is alive after the call …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用类型unique_ptr的自定义删除器SDL_Surface.这只是一个使用int类型的例子,但我希望你能得到这个想法.
#include <iostream>
#include <functional>
#include <memory>
typedef int SDL_Surface;
SDL_Surface * CreateSurface()
{
SDL_Surface * p = new SDL_Surface;
return p;
}
void FreeSurface(SDL_Surface *p)
{
delete p;
}
int main() {
std::unique_ptr<SDL_Surface, std::function< void (SDL_Surface *) > > uptr_1;
//how to assign a value to uptr_1 and the deleter?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否uptr_1正确声明并初始化为nullptr?如果是这样,我如何分配指针和删除功能?
我怎么能封装这个:
std::unique_ptr< SDL_Surface, std::function< void (SDL_Surface *) > >删除器并不总是在SDL_Surface我想要的每一行上写下那行,另一个是typedef?
我刚开始学习C++ 11的功能,这对我来说很难.
我不想写每个使用shared_ptr或unique_ptr的类:
std::shared_ptr<Foo> p = CreateFoo();
Run Code Online (Sandbox Code Playgroud)
我正在使用这个:
template <typename T>
struct ptr_types
{
typedef std::shared_ptr<T> sptr;
typedef std::unique_ptr<T> uptr;
};
class A: public ptr_types<A>
{
public:
A(){}
int m;
};
Run Code Online (Sandbox Code Playgroud)
然后,我可以这样做:
A::sptr p(new A);
Run Code Online (Sandbox Code Playgroud)
从设计的角度继承使用智能指针的类是一个问题吗?有更优雅的解决方案吗?
编辑:
是的我可以使用:
auto p = std::make_shared<A>();
Run Code Online (Sandbox Code Playgroud)
但如果我有这个怎么办:
std::shared_ptr<A> A::CreateA()
{
....
}
void A::Sink(std::unique_ptr<A> p)
{
...
}
Run Code Online (Sandbox Code Playgroud)
有更好的东西吗?
A::sptr A::CreateA()
{
...
}
void A::Sink(A::uptr p)
{
...
}
Run Code Online (Sandbox Code Playgroud)
也许这个问题没有意义..而且我一直懒得在函数类型返回或参数中编写std :: blabla.
我有一个回调系统,当发生某些事情时,它会保存lambda.要获得通知,您必须将lambda注册到标识符,或者如果您不想再次通知则取消注册.
我遇到的问题是我已经注册了lambdas,在被调用时将从该系统取消注册,导致正在执行的当前lambda的销毁.而且我认为这不安全.但我不确定.
简化例如:
#include <map>
#include <functional>
#include <iostream>
int main() {
std::map<int, std::function<void ()>> m;
m[10] = [&m] () {
int i = m.size(); // Just cheking the internal state of the lambda
m.clear();
//i += m.size(); // If I uncomment this the std::cout is not working.
std::cout<< "What happens? " << i << std::endl;
};
m[10]();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我所看到的是,当我检查lambda的状态后m.clear(),我得到了奇怪的行为(例如std::cout不工作).你能解释一下这些案件究竟发生了什么吗?
你将如何处理回调的情况?在调用回调之前制作副本(似乎是禁止的)?
我正在努力使用std :: mt19937生成器作为类成员,但我总是得到相同的结果.这是我正在尝试的一个例子.
class Level
{
public:
Level();
private:
int generateTokenType();
std::mt19937 m_mt;
std::random_device m_randomdevice;
};
Level::Level(): m_mt(m_randomdevice())
{
}
int Level::generateTokenType()
{
std::uniform_int_distribution<int> dist(0, 10);
return dist(m_mt);
}
Run Code Online (Sandbox Code Playgroud)
我想要的是维护生成的发生器并在程序执行期间询问数字.
- 编辑 - 按照Cornstalks的回答,我做到了:
class Level
{
public:
Level();
private:
int generateTokenType();
std::mt19937 m_mt;
};
Level::Level(): m_mt((std::random_device())())
{
for(auto i = 0; i < 10; i++)
std::cout<<generateTokenType()<<" ";
std::cout<<std::endl;
}
int Level::generateTokenType()
{
std::uniform_int_distribution<int> dist(0, 10);
return dist(m_mt);
}
Run Code Online (Sandbox Code Playgroud)
但是在每次执行时我得到相同的数字......
我使用右手坐标(x =右,y =向上)将我的物体放在世界空间中.
当我必须使用SFML渲染它们时,我遇到了问题,因为我无法在sf :: View中使用(y = up)矩阵设置View矩阵,然后全部渲染y-flipped.
我正在考虑的一个解决方案是在渲染之前在每个对象上翻转y轴:
ObjectTransformMatrix * MatrixScale(1.0f,-1.0f)
Run Code Online (Sandbox Code Playgroud)
但我想我必须将sf :: View中心移动到:
y = y - (view_size.y / 2.0)
Run Code Online (Sandbox Code Playgroud)
为什么sf :: View是y-inverted?我的解决方案是否正确