I'm using QTCreator as a code editor for my C++ project, not using the real features of the qmake compilation process.
My project has several subdirectories, in all of which I ran qmake -project to create a duummy .pro file that simply lists the source and header files in the directory.
In my root folder, I simply created a "main.pro" file that includes all these "subdir/subdir.pro" files.
So it looks like this:
./
main.pro
subdir1/
/include
/src
subdir1.pro
subdir2/
/include
/src
subdir2.pro
Now my problem is, I use some files that have a special file extension (say, .ccp), which are actually some C code but are used in a different step of my compilation process.
They are naturally ignored by the qmake -project command and do not appear in my project.
I read here that I could use the qmake setting QMAKE_EXT_CPP to tell it to gather my files as a C-code file, but it doesn't seem to be working.
If I run qmake -query QMAKE_EXT_CPP, I get .cpp::.c::.ccp (which I set right before), but when running a new qmake, it doesn't take my .ccp files in account.
So, three questions:
Is it possible to make qmake take some special extensions as a C++ file, when building the .pro file?
If yes, is it correct to use the QMAKE_EXT_CPP setting?
If yes, what should be the syntax of the QMAKE_EXT_CPP setting? (mine inspired by this forum post, but it might be bogus).
You cannot change QMAKE_EXT_CPP with -project option. The list of cpp extensions used at this stage is hardcoded into qmake. However after initial creation of .pro file you can edit it to extend with support for other extensions:
in test.pro
QMAKE_EXT_CPP += .ccp
SOURCES += test.ccp
You have to add new files manually.
Related
I have two executables that are build from the same source (a client and a server) and they're built with the compile options -D CLIENT=0 -D SERVER=1 for the server and -D CLIENT=1 -D SERVER=0 for the client. If I do something like
if (CLIENT) {
// Client specific code
}
clangd complains that CLIENT is not defined. Is there a way to make clangd aware of those macros? (The code compiles just fine, the errors are from clangd, not the compiler)
Is there a way to make clangd aware of those macros?
From getting started with clangd:
Project setup
To understand source code in your project, clangd needs to know the
build flags. (This is just a fact of life in C++, source files are not
self-contained.)
By default, clangd will assume that source code is built as clang
some_file.cc, and you’ll probably get spurious errors about missing
#included files, etc. There are a couple of ways to fix this.
compile_commands.json
compile_commands.json file provides compile commands for all source
files in the project. This file is usually generated by the build
system, or tools integrated with the build system. Clangd will look
for this file in the parent directories of the files you edit. Other
tools can also generate this file. See the compile_commands.json
specification.
compile_commands.json is typically generated with CMake build system, but more build systems try to generate it.
I would suggest moving your project to CMake, in the process you will learn this tool that will definitely help you in further C-ish development.
compile_flags.txt
If all files in a project use the same build flags, you can put those
flags, one flag per line, in compile_flags.txt in your source root.
Clangd will assume the compile command is clang $FLAGS some_file.cc.
Creating this file by hand is a reasonable place to start if your
project is quite simple.
If not moving to cmake, create a compile_flags.txt file with the content for example like the following, and clangd should pick this file up:
-DCLIENT=1
-DSERVER=1
I am working to build a Code Composer Studio project using cmake, which is new to me. It builds successfully under Linux but I am struggling to get it to work under Windows. The cmake command executes without issue, but make fails during the very first C object at the very first #include with the error code
fatal error: could not open source file "stdbool.h" (no directories in search list)
I'm using the libraries included in CCS's compiler (c6000_7.4.15), and that whole folder is included in the CSS project. I include it in cmake as well. In my .cmake file:
set (CCS_ROOT ${CCS_ROOT_V6_WIN} CACHE PATH "code composer install directory")
set(CGT_COMPILER_ROOT ${CCS_ROOT}/tools/compiler/c6000_7.4.15 CACHE INTERNAL "DSP Compiler Root")`
And in the CMakeLists.txt file:
set (COMPILER_INCLUDE ${CGT_COMPILER_ROOT}/include)
INCLUDE_DIRECTORIES ("${COMPILER_INCLUDE}")
Why can the header files not be opened when they're linked in the project and CMake can find them just fine?
EDIT: The directory structure had been changed underneath me, so I took the opportunity to add all of the external files directly into the project to make it completely platform-independent. That way, since the project is managed by our Git repository, users won't have to install the CSL or any other programs to build the project. This also means that paths to libraries and header files will never change between revisions and environments.
Unfortunately, this has not solved my problem. The project continues to build in Linux while failing to ind the very first included header file. I also notice that, under Windows, it cannot find my own header files unless I provide a relative path, e.g. #include "../Common.h" I can get make to find stdbool.h if I provide an absolute path to the compiler directory, but that exposes a web of additional broken links between files.
As a side note, the project builds successfully within Code Composer Studio, so I am assuming that this isn't an issue with my specific Windows environment nor with the code within the project itself.
This seems to be an issue with gcc.exe. I set an environment variable CC to the path of a different compiler (in my case a TI compiler) within my build script and that fixed the problem.
How does one properly use file paths in source code (relative to the project root) when building with cmake?
Details:
I have a cmake project with basically this layout:
project
|-src/
| |-main.c
| |-CMakeLists.c
|-dat/
| |-foo.txt
|-build/
main.c basically contains a main function that tries to open a file, specified relative to the project's root directory, i.e., dat/foo.txt. When building with cmake, the binaries are located in project/build/src, which is also the working directory when running the program. dat/foo.txt is interpreted as project/build/src/dat/foo.txt which obviously is not available. One could replace the path in the source file with ../../dat/foo.txt, which however is not at all a clean and nice solution.
This stackoverflow post states that there is no easy way for setting the working directory of code executed by cmake (furthermore, the answer refers to a solution for Visual Studio, which I am not using).
What is the best practice for this problem?
My project's directory structure is basically as follows:
root/src
root/assets
root/library
I currently have CMake set up to compile the source, compile the library, and then link them, by calling make from the root directory.
I then have to manually move the executable into the original assets directory to get it to run, since that's where it expects to be (and we want to test with our directory structure in assets as close to what we expect it to be when it's done).
So, is there any way to tell CMake to automatically stick the compiled binary in that directory, as well as copy the assets over? Since we're doing out of source builds, sticking the executable back into the original project source's assets folder seems odd.
In short, two questions: Is there any way to get CMake to copy assets as well as code, and is there any way to have it copy the generated executable to a specific location in the build tree?
Any help would be appreciated --- thank you!
Here's a simple example with a structure like yours:
root/src/main.cpp (only source file)
root/assets (where I want the executable to go)
Here's the cmake file:
PROJECT(HelloCMake)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${HelloCMake_SOURCE_DIR}/assets)
add_executable (HelloCMake src/main.cpp)
When I build against this using Visual Studio I get the output placed in root/assets/debug. I'd have to dig to figure out how to get rid of the extra configuration folder (debug). Not perfect, but hopefully that gets you on the right track.
Edit...Even better:
INSTALL(TARGETS HelloCMake DESTINATION ${HelloCMake_SOURCE_DIR}/assets)
I'm working on a relatively big project that is using automake build system.
Now the problem is that I need to link the project with a library from another project (this works fine), but I also need to include a header from the other project source tree (api.h).
INCLUDES = -I#REMOTE_PROJECT_DIR#
in Makefile.am doesn't work, because there are .h files with coliding names in the remote source directory. How can I add just the api.h?
I used a symlink into the include directory in the project, but now I need to push the sources into a public repo and everyone working with it has the other project in a different directory, so I need to use the configure param.
You do not want to tweak you Makefile.am or your configure.ac in any way. If api.h is installed in a standard location (eg /usr/include), then all you need is AC_CHECK_HEADERS([api.h]) in configure.ac. If api.h is installed in a non-standard location (eg /home/joe/include), the way to pick it up in your project is determined at configure time. When you run configure, you add the argument CPPFLAGS=-I/home/joe/include to the invocation of configure. You do not indicate the non-standard location in the build files themselves.
Another alternative is to use pkg-config, but the non-standard location of your header file will still be dealt with when you run configure. (This time by setting PKG_CONFIG_PATH rather than CPPFLAGS)
If you have headers with same names, you could put at least one of them into directory with different name and include it using directory name.
Here's a sample directory structure:
mylibrary/include/myblirary/api.h
myproject/api.h
myproject/main.cpp
In main.cpp:
#include "api.h"
#include "mylibrary/api.h"
#include <boost/regex.hpp>
When compiling:
g++ -I mylibrary/include