cmake代码install(TARGETS mylib mylib_shared ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin INCLUDES DESTINATION include EXPORT MyLibTargets) 在执行时提醒 export Export set "MyLibTargets" not found.
时间: 2024-02-10 18:04:14 浏览: 70
这个错误提示是因为在执行 `install` 命令时,指定了一个名为 `MyLibTargets` 的导出配置,但是在之前的代码中并没有定义该导出配置。
解决这个问题,需要在 CMakeLists.txt 文件中添加一个 `export` 命令,用于定义 `MyLibTargets` 导出配置。例如:
```
# 定义导出配置
export(TARGETS mylib mylib_shared FILE MyLibTargets.cmake)
# 安装配置文件和导出配置
install(TARGETS mylib mylib_shared
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
EXPORT MyLibTargets)
install(EXPORT MyLibTargets
FILE MyLibTargets.cmake
DESTINATION lib/cmake/MyLib)
```
这样,就会在安装时生成一个名为 `MyLibTargets.cmake` 的导出配置文件,并将其安装到 `lib/cmake/MyLib` 目录下。之后,在使用该库时,可以通过 `find_package(MyLib)` 命令加载导出配置文件,从而使用库。
相关问题
cmake中install(TARGETS mylib mylib_shared EXPORT MyLibTargets ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin)如何指定MyLibTargets中库的搜索路径
"
android:layout_below="@id/description_text_view"
android:layout_toRightOf="@id/image_view"
android:layout_marginLeft="8dp"
android:textSize="12sp"
android:textColor="@color/colorAccent"/>
</RelativeLayout>
``你可以在使用`install()`函数中指定`EXPORT`选项时,通过`NAMESPACE`子选项来为导出`
创建 NewsItem 类,它包含新闻的标题、描述、URL、图片 URL 和发布日期。
NewsItem.java:
的目标指定一个命名空间,然后使用`install(EXPORT)`函数导出目标时,指定````java
public class NewsItem {
private String title;
private String description;
private String url;
private String imageUrl;
DESTINATION`选项来指定导出文件的输出路径。在使用导出文件的项目中,可以使用`find_package private String publishedAt;
public NewsItem(String title, String description, String url, String imageUrl, String publishedAt) {
this.title = title;
this.description = description;
this.url = url;
this.imageUrl = imageUrl;
this.publishedAt =()`函数并指定`<MyLibName>_DIR`变量来指定导出文件所在的路径。下面是一个 publishedAt;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
示例:
在`CMakeLists.txt`文件中:
```cmake
# 导出目标
install(TARGETS mylib }
public String getUrl() {
return url;
}
public String getImageUrl() {
return imageUrl;
}
public mylib_shared
EXPORT MyLibTargets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION String getPublishedAt() {
return publishedAt;
}
}
```
创建 NewsDetailActivity 类,它显示单个新闻 bin)
# 指定导出文件输出路径
install(EXPORT MyLibTargets
NAMESPACE MyLib::
DESTINATION lib/c的详细信息,包括标题和 URL。
NewsDetailActivity.java:
```java
public class NewsDetailActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_detailmake/MyLib)
# 在使用导出文件的项目中,通过 find_package() 函数来查找库
find_package(MyLib);
String title = getIntent().getStringExtra("title");
String url = getIntent().getStringExtra("url");
getSupportActionBar().setTitle(title);
REQUIRED)
target_link_libraries(myapp MyLib::mylib)
```
在导出文件 `MyLibTargets.cmake` 中:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
webView = findViewById(R.id.web_view);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl```cmake
# 设置导出文件版本号
include(CMakePackageConfigHelpers)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY(url);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id_DIR}/MyLibConfigVersion.cmake"
VERSION ${MyLib_VERSION}
COMPATIBILITY SameMajorVersion)
# 将目标.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
```
activity_news_detail放入命名空间
export(EXPORT MyLibTargets
NAMESPACE MyLib::
FILE "${CMAKE_CURRENT_BINARY_DIR}/.xml:
```xml
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
最后,创建一个MyLibTargets.cmake")
# 生成配置文件
configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/MyLibConfig.cmake.in Service 类,它显示一个定期的通知,提醒用户查看最新的新闻。
NewsNotificationService.java:
"
"${CMAKE_CURRENT_BINARY_DIR}/MyLibConfig.cmake"
INSTALL_DESTINATION lib/cmake/MyLib)
# 安```java
public class NewsNotificationService extends Service {
private static final int NOTIFICATION_ID = 1;
private static final String装配置文件
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/MyLibConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/My CHANNEL_ID = "news_notification_channel";
private static final String CHANNEL_NAME = "News Notification Channel";
private static final String CHANNEL_DESCRIPTIONLibConfigVersion.cmake"
DESTINATION lib/cmake/MyLib)
```
在配置文件 `MyLibConfig.cmake.in` = "Shows news notifications to the user";
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
@ 中:
```cmake
# 指定库的搜索路径
set(MyLib_DIR @CMAKE_INSTALL_PREFIX@/lib/cmake/Override
public void run() {
showNotification();
handler.postDelayed(this, 60 * 60 * 1000); // repeat every hour
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId)MyLib)
# 通过 find_package() 函数查找库
find_package(MyLib REQUIRED)
# 将库的头文件路径添加到 {
createNotificationChannel();
handler.postDelayed(runnable, 60 * 60 * 1000); // start after 1 hour include 目录中
include_directories(${MyLib_INCLUDE_DIRS})
```
这样,在使用 `find_package(MyLib)` 函数时,
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
就会自动查找到 `MyLibTargets.cmake` 文件,并将 `MyLib` 目标添加到命名空间 }
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL `MyLib::` 中。在使用 `target_link_libraries(myapp MyLib::mylib)` 命令时,就可以通过_ID, CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(CHANNEL_DESCRIPTION);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
private void showNotification() {
Intent intent = new Intent命名空间 `MyLib::` 来引用 `mylib` 目标,并且不需要手动添加库的搜索路径。
代码install(TARGETS mylib mylib_shared EXPORT MyLibConfig ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin INCLUDES DESTINATION include) install(FILES include/header.h DESTINATION include),如何在导出的MyLibConfig添加变量设置,即set(MyLib_VERSION @MyLib_VERSION@) set(MyLib_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/include") set(MyLib_LIBRARIES "@CMAKE_INSTALL_PREFIX@/lib/libmylib.a") set(MyLib_LIBRARIES_SHARED "@CMAKE_INSTALL_PREFIX@/lib/libmylib_shared.so")中
你可以将这些变量设置添加到MyLibConfig.cmake.in文件中,该文件应该位于你的项目根目录下的cmake文件夹中。MyLibConfig.cmake.in文件应该类似于以下内容:
```
# check if the package has been loaded already
if(NOT TARGET MyLib::mylib)
include("${CMAKE_CURRENT_LIST_DIR}/MyLibTargets.cmake")
endif()
# set version and include directories variables
set(MyLib_VERSION @MyLib_VERSION@)
set(MyLib_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/include")
# set library variables
set(MyLib_LIBRARIES "@CMAKE_INSTALL_PREFIX@/lib/libmylib.a")
set(MyLib_LIBRARIES_SHARED "@CMAKE_INSTALL_PREFIX@/lib/libmylib_shared.so")
```
当你运行`install(EXPORT MyLibConfig ...)`命令时,CMake将自动将这些变量设置添加到MyLibConfig.cmake文件中。请记住,你需要将MyLibConfig.cmake.in文件名用MyLibConfig.cmake替换,以便CMake能够正确加载它。
阅读全文