I am using the terminal and gedit editor to learn QT C++. Do not mind my approach of programming, I do not like to depend on IDE's, but plain text editors. However I seem to be having some issues compiling. I created a basic simple snippet, then after saving as a .cpp extension file I type the command lines;
qmake -project
qmake
make
qmake main.cpp
All this is being done in a directory folder on my desktop called "qt". I try to run the file with:
qmake main.cpp
and all it shows is: Unknown test function: int main.
I check the error and the int main looks fine. The int main contains two arguments an int argc, and a char *argv[] which is passed to the QApplication() function parameters in the local scope. Obviously I am doing something wrong and cannot seem to figure out what.
Also as I mentioned above about using a .cpp extension, I also tried replacing the .cpp with .pro that also did not seem to work out, it displayed the same exact errors.
Related
This has to be some kind of newbie question, but I have not been able to find any explaination.
I am running Ubuntu 18, and need to work with some C/C++ files. I've been using TI's CCS which is eclipse based on Windows for years.
I downloaded the Eclipse installer and ran it setting up for C/C++ developers.
https://www.eclipse.org/downloads/packages/installer
I created a new project. There were several different (unexplained) options such as CDT, MESON, MakeFile, ... I have tried several.
Creating a HelloWorld source file, it compiles and runs fine.
#include <stdio.h>
int main() {
puts("Hello World");
return 0;
}
Okay, so far...
Now I add a new source file. Called "OtherFile.c"
#include <stdio.h>
void OtherFunction() {
puts("Other Hello");
}
And of course, modify the original:
#include <stdio.h>
extern "C" void OtherFunction();
int main() {
puts("Hello World");
OtherFunction();
return 0;
}
When I try to build, it will not compile the new file. And (as expected) it tell me that "OtherFunction" is unresolved.
I have tried multiple project types (CDT, Meson, Makefile) even though there is no explanation of the differences. The newer file will not be compiled.
I tried changing the file extension from c to cpp and back. The newer file will not be compiled.
The TI version of CCS using Eclipse will include a source file when it's in the folder. However, in this environment, I cannot convince Eclipse to compile any other file than the one that was originally created by the new C/C++ project step.
And just as annoying is the fact that I can't right click either file and "Build Selected File". The menu option doesn't even appear.
This did not work for me:
eclipse c/c++ CDT build just one file
Can someone advice how to convince Eclipse to compile additional files?
TIA.
EDIT:
I can't upload here, so I just created something on GitHub.
These are two of the samples where I added a second file, and it ignores it.
https://github.com/scotty2541/EclipseExample
In all the other things I've done in Eclipse, it simply uses a default "recipe" like make does to compile the file.
If there is some way to manually tell Eclipse about it, that isn't explained anywhere I've been able to find. And seems to defeat the purpose of the IDE's behavior.
I was able to get it to behave as expected: By choosing a CDT managed build system, when adding a file to the project, it compiles it using the default recipe
Then, there is a setting which causes it to run the "builder" after a clean.
When I added the file as described originally, I also had to do a "clean" in order for the environment to include the additional file.
I am compiling scientific code that performs numerically intensive calculations. My process is the following:
Code targeted for the CPU is compiled by the Intel C++ Compiler
Code targeted for the GPU is compiled by NVCC
The object files are then linked using the Intel C++ Compiler
To do this, I have written a makefile to perform the necessary steps, and everything is carried out on the command line. Now, I wish to add on a GUI to the program, using Qt, but without using Qt Creator. As a test, I am trying to compile the "Hello World! Desktop application" given here: https://wiki.qt.io/Getting_Started_on_the_Commandline
My interpretation is as follows:
#include <QtGui\QtGui>
#include <QtWidgets\QApplication>
#include <QtWidgets\QLabel>
void test_qt()
{
QApplication app();
QLabel label("Hello, world!");
label.show();
app.exec();
}
I call the function in a main.cpp file. In my makefile, I link with Qt5core.lib, Qt5Gui.lib and Qt5Widgets.lib, and as per the makefile rules, this is compiled with the Intel C++ compiler. However, it gives the following error:
error: expression must have class type
app.exec();
^
My question is as follows:
How can I edit my makefile to compile Qt code? I will be needing signals and slots, so moc may be needed according to Can I use Qt without qmake or Qt Creator? but I will not be using uic.
Update based on the discussion below:
Looking at the QApplication constructor reference, it would appear that it is initialized with the command line arguments argc and argv obtained in main(). This makes it possible to pass Qt-specific flags to the Qt infrastructure. Thus, it cannot be created with no arguments as in your example -- you need to pass argc and argc from main.
As for compiling the files containing signals and slots - these features are not standard C++, so they need to be preprocessed by a tool that knows what they mean. If I understand correctly, moc converts these Qt-specific features into standard C++ code which must then be compiled using your compiler. So:
Use a naming convention for your Qt-specific cpp files so you can create a makefile pattern to process them with moc. ex: file.moc.cpp
Create a dependency on file.cpp in your makefile, which depends on file.moc.cpp
Create a rule for creating .cpp files from .moc.cpp files and invoke moc in that rule.
Call your normal compiler on the .cpp files. Don't forget your include directories, etc.
Make sure you do not check in these generated files, as they will change each time your .moc.cpp file changes. Maybe dump them in a temp directory that is ignored by your revision control? You may want to search around to see how other sample projects do it.
Side Note: declaring app without the parentheses could lead to a case of The Most Vexing Parse.
https://en.wikipedia.org/wiki/Most_vexing_parse
When an object is declared, but the parentheses are empty, the compiler ends up interpreting the line as a a function prototype for a function that takes no arguments and returns a QApplication.
I have created an empty project and added two ( .cpp) items inside the project. Keep in mind that I am a beginner in C++ Visual Studio, so I have not used any code to somehow connect the two files.
Problem: The debugger was fine for debugging my first file, but when I open my second file and start running by clicking "Local Windows Debugger" (clicked when I was inside the second file), it will still keep on running the first file whether there was a bug or not.
When I looked at the debug window after I hit "Local Windows Debugger", I saw the file path pointing to the first file.
I have tried: Closing the first file completely, closing visual studio and opening my second file from my folder path, turning off the break-point in the first file and turning on in the second file.
I would like to know: How can I just run the second file? Do I have to use the command prompt to keep my two items in the empty project separate? I am using Windows 10 by the way.
I searched for my problem, but I had a hard time looking for a guide that gave a solution
Issue:
Dracep cannot debug one of two files they have. This is due to them having a main function definition in both files, causing the editor to favour one over the other.
Solution:
By having a third, dedicated entry point in your application (I.E. having a single point of entry you then include the other files), you can decide which file you are going to debug at any given time.
For example, having a file called main.cpp which then includes the other two files by using #include "filename.h".
From there you can include the file and make you code checks by calling the functions in that file rather than having a main and stepping down through it, causing long term issues of scalability.
Please see this question on separating your logic from your definitions, as the answer marked correct is the standard for most C++ projects you will find.
That way, you could do something like the following:
#include "File1.h"
#include "File2.h"
int main(int argc, char** argv)
{
File1Class file1Class;
File2Class file2Class;
//Do whatever tests you like with either.
}
My program is extremely simple and I'm just learning how to work with prototypes by making header files.
My folder setup is like this:
other.cpp
other.h
entry.cpp
My file, entry.cpp, looks like this:
#include "other.h"
int main(){
doSomething();
return 0;
}
doSomething is a function declared in other.cpp and is prototyped in other.h like so:
//ifndefine pre-processor statements
void doSomething();
Building is where I run into trouble. Runningg++ main_c_file.cpp -o name.exe in the console is supposed to create the executable. The problem I am having is that it doesn't know what doSomething is.
I suspect it's because the processor is not searching for other.cpp. I assumed, based on the tutorial I am following, that I didn't actually need to specify this file; it worked fairly well in their video to simply include just the header file and have the cpp file in the same directory.
further: I downloaded their source code and the problem remained. It is not my code, it is how I am trying to build my files. I am not using an IDE to build my programs (I am using Atom IDE to write programs if you know of a shortcut through there), and like stated earlier I am just running the Mingw g++ cppfile.cpp -o command.
Besides the straight answer, if there is a way that can make building cpp projects easier please link me to it or write it here. I have had nothing but trouble in the past trying to learn this language.
I have to develop my project in text-mode debian linux. I'm using Vim and I installed the clang_completion plugin on it. I made .clang_completion file in root of my project :
-I.
-I/usr/include
-I/usr/include/c++/4.6
When I write a program like below, the completion works fine.
//#include <stdio.h>
int main()
{
struct A
{
int x, y;
};
A a;
a. // After putting dot, the suggestion popup appears
return 0;
}
However, after removing the comment of first line, it doesn't work! How can I overcome this issue?
I found the easiest way to get clang_complete to work is to use the provided cc_args.py file.
when compiling a project use clang_complete/bin/cc_args.py instead of gcc/g++
This will generate the correct .clang_complete file with all libraries and dependencies.
Provided the clang_complete source directory in your home folder.
Example Makefile:
CXX=$(HOME)/clang_complete/bin/cc_args.py g++
all:
$(CXX) main.cpp
I've successfully used the clang_complete plugin in the past (now I just use cscope and ctags, which I consider enough).
Including external headers worked fine in my configuration, but, as the clang complete plugin page specifies, the file in which to put include paths (or any other flag you may want to pass to the clang compiler), must be named .clang_complete and not .clang_completion.
Also, I used to put the options on a single line, just as I was going to pass the plain content of the .clang_complete file as a command line option (don't know if separating lines with \ will work).
Hope this helps.