我想做的是:
我尝试了这段代码(快速测试案例):
的CMakeLists.txt
PROJECT (minimalcpp)
CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
SET(LIBSAMPLE_HEADERS func_simple.h)
SET(LIBSAMPLE_SRCS func_simple.cpp)
ADD_LIBRARY (minimalcpp SHARED ${LIBSAMPLE_SRCS})
ADD_EXECUTABLE (test-pure-cpp test-pure-cpp.cpp)
TARGET_LINK_LIBRARIES (test-pure-cpp minimalcpp)
# THIS WORKS BUT IT IS NOT WHAT I WANT :
# ADD_EXECUTABLE (test-pure-cpp test-pure-cpp.cpp ${LIBSAMPLE_SRCS})
macros.h
#ifndef MACROS_H
#define MACROS_H
#if defined _WIN32 || defined __CYGWIN__
#if minimalcpp_EXPORTS
#define MINIMALCPP_API __declspec(dllexport)
#else
#define MINIMALCPP_API __declspec(dllimport)
#endif
#endif
#endif
func_simple.h
#ifndef LIB_SAMPLE_FUNC_SIMPLE_H
#define LIB_SAMPLE_FUNC_SIMPLE_H
#include "macros.h"
namespace sample {
MINIMALCPP_API void f1(int nmax);
}
#endif // LIB_SAMPLE_FUNC_SIMPLE_H
func_simple.cpp
#include <iostream>
#include "func_simple.h"
void sample::f1(int nmax) {
int i ;
for(i=0 ; i < nmax ; i++) {
std::cout << i << " -> " << i*i << std::endl;
}
}
测试纯cpp.cpp
#include "func_simple.h"
int main(int argc, char *argv[]){
sample::f1(5);
return 0;
}
此代码编译但在执行时直接崩溃。
错误讯息:
Le programme s'est terminé subitement.
... a quitté avec le code -1073741515
我是Windows上的C ++初学者,我做错了什么? 非常感谢你
答案 0 :(得分:0)
见[g-makulik]回答:
在这种情况下,此消息表示找不到dll,这就是应用程序崩溃的原因。 一种解决方案是说cmake将dll和可执行文件放在bin中的相同目录中,例如:
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
此约定用于Qt等库中。