Have CLion 1.2.2.
Made simple project:
main.cpp
a.hpp
a.cpp
b.hpp
b.cpp
What I want now is to move a.hpp/a.cpp into separate library (sub)project, same
for b.hpp/b.cpp and refer to them in main project CMakeList.txt
Is there a good way to do that?
More general question: how to handle several library (sub)projects which could be shared between quite a few final projects ("solutions" in Microsoft speak)
Found docs on project management in CLion seriously lacking...
In CLion, project management is currently done by editing the CMakeLists.txt file. Everything CLion shows you comes from parsing that file.
Change your CMakeLists.txt from something like:
add_target(foo main.cpp a.cpp a.hpp b.cpp b.hpp)
to something like:
add_library(foolib a.cpp a.hpp b.cpp b.hpp)
add_target(foo main.cpp)
target_link_libraries(foo foolib)
Related
I'm new in C. My code uses termcap library. And I'm trying to debug my code using Clion through Cmake Application. But it can't be compiled because functions that i use from a library are undefined. What should i add to CMakeLists.txt to debug my project?
My CMakeLists.txt now:
cmake_minimum_required(VERSION 3.21)
project(minishell C)
set(CMAKE_C_STANDARD 99)
add_executable(minishell main.c)
When i compile using clang i just do this:
clang main.c -ltermcap
and it works.
Can't understand what to do. Please help.
I want to create a static library libmylib.a from mylib.c/.h and link it to a project to use this library in bootloader code using the arm-none-eabi-gcc cross compiler in ubuntu 20.04 LTS.
I have an electronic engineering background, so I'm kind of new in this compiler and linker stuff.
What I know:
I've been searching about this, and found out that '.a' are just packed '.o' files, and that's it. You can do it using ar in linux. I don't know how to manage the dependencies for this '.a' file, for example, or how to link it to the project.
What I want to know:
I really want to understand how it works, to compile and generate the bin, elf or hex files using these static libraries for arm using the arm-none-eabi-gcc cross compiler (found some for linux), but I don't know how to search for this properly, how to learn it in a linear way. If you guys could help me on this I would be really grateful.
First you create your library objects. Let us say that you have a foo function written in foo.c, then you do:
arm-none-eabi-gcc -c foo.c
The -c options tells the compiler to stop after assembling and no go further.
Then you need to create the .a file
arm-none-eabi-ar -rc libfoo.a foo.o
this command creates a static library called libfoo.a
At the end you compile your main with:
arm-none-eabi-gcc -L. -lfoo main.c -o main
Note that in -l flag we don put "lib" and ".a", those are automagically added. The -L. flag tells gcc to look into the current folder for library files.
I have a demo project and it's structure like as below:
top_dir
CMakeLists.txt
sub_dir1
CMakeLists.txt
sub_dir2
CMakeLists.txt
top_dir/sub_dir1/CMakeLists.txt used to build lib1 by using add_library(lib1 ...),
top_dir/sub_dir2/CMakeLists.txt used to build exe1 with linking lib1 by target_link_library(exe1 lib1).
And the content of top_dir/CMakeLists.txt is as below:
add_subdirectory(sub_dir2)
add_subdirectory(sub_dir1)
Normally, when build target exe1, cmake will check dependency so lib1 will be built before building exe1. The problem is I am transfering an existed makefile project into CMake, and there are many gcc link options, like "whole-archive ... no-whole-archive, allow-mutiple-definition", if use like target_link_library(exe1 "-Wl, --whole-archive ../sub_dir1/liblib1.a --no-whole-archive")(The form like this, and this may not work, it just a e.g.), cmake seem don't built lib1 any more. Is there any way i can use target_link_library like target_link_library(exe1 "-Wl, --whole-archive ../sub_dir1/liblib1.a") and cmake link dependency checking still work, or other way i can transfer these gcc link options into cmake?
Arguments for target_link_libraries are going into the resulted command line in the same order they appears. Whenever target name is used as argument, path to target's output is used in resulted command line. So, you may use library target whenever you need path to that library in the command line:
target_link_libraries(exe1 -Wl,--whole-archive lib1 -Wl,--no-whole-archive)
Such a way a target-level dependency between executable exe1 and library lib1 is automatically deduced by CMake, as usual.
The next hack permits to locally define the flags to the library to which you want to apply the flags, without modifying all exe link flags :
add_library(lib1_internal STATIC lib1.cpp)
add_library(lib1 STATIC dummy.cpp) # dummy.cpp is an empty file
target_link_libraries(lib1 PRIVATE -Wl,--whole-archive lib1_internal -Wl,--no-whole-archive )
....
target_link_libraries(exe1 lib1)
I have a problem with PCL: specifically I want to use it in the existing project with existing Makefiles. However, PCL is using CMake and I couldn't find how to add it to Makefile directly. Does anyone know how to do that?
First try to compile the one of the example provided in PCL website using CMake.
http://pointclouds.org/documentation/tutorials/pcl_visualizer.php
After compiling the above example, you will find various new files and a folder created by CMake in your directory.
Go to CMakeFiles/pcl_visualizer_demo.dir/ .
Open file named link.txt, which contains the terminal command which has various pcl(point cloud libraries) linked dynamically to the file.
command should look similar to the command shown below
/usr/bin/c++ -O3 -Wno-deprecated -s CMakeFiles/pcl_visualizer_demo.dir -o pcl_visualizer_demo -rdynamic -lpcl_common -Wl,-Bstatic -lflann_cpp_s -Wl,-Bdynamic -lpcl_kdtree -lpcl_octree -lpcl_search -lqhull -lpcl_surface -lpcl_sample_consensus -lpcl_io -lpcl_filters -lpcl_features -lpcl_keypoints -lpcl_registration -lpcl_segmentation -lpcl_recognition -lpcl_visualization -lpcl_people -lpcl_outofcore -lpcl_tracking /usr/lib/libvtkGenericFiltering.so.5.8.0 /usr/lib/libvtkGeovis.so.5.8.0 /usr/lib/libvtkCharts.so.5.8.0 /usr/lib/libvtkViews.so.5.8.0 /usr/lib/libvtkInfovis.so.5.8.0 /usr/lib/libvtkWidgets.so.5.8.0
You can include these libraries in your Makefile directly.
If you use different functions or pcl headers files, then first try compiling it using CMake and get the libraries linked and add it to your Makefile of previous project.
I tried this method for my project which worked perfectly fine. I tried pkg-config to link the libraries, which didn't work in my case. I was not able to find any other method that easily links all the required libraries.
I have an Objective-C/Cocoa project that incorporates a static library. That static library has some object files that have C++ in them.
I've found that if the project that I'm using the library in contains no other C++ in it, the link fails (can't link new/delete/etc). But simply adding a single (empty) .cpp file to the project causes the link to succeed.
In practice, what happens is that the build will invoke g++ instead of gcc when there is any cpp, which succeeds. No other difference in the build is apparent to me.
Is there an explicit switch I can use to link in this library without using the dummy cpp file in the project?
(This is mostly a curiosity question-- it's not the end of the world to put in one empty file. :) )
Thanks.
try to link libstdc++
gcc main.c -lstdc++
or in Xcode:
Project->Edit Project Settings
To the config section "Other Linker Flags", add -lstdc++.