cmake中install(TARGETS mylib mylib_shared EXPORT MyLibTargets ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin)如何指定MyLibTargets中库的搜索路径
时间: 2024-02-10 13:04:11 浏览: 143
test_lib.tar.gz
"
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` 目标,并且不需要手动添加库的搜索路径。
阅读全文